from flask import Flask, request, jsonify import cv2 import threading import time app = Flask(__name__) # 设置存储照片的目录路径 photo_path = '/Desktop/cars/photo/' # 初始化摄像头 camera = cv2.VideoCapture(-1) camera.set(cv2.CAP_PROP_FRAME_WIDTH, 640) camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 480) # 初始化变量 photo_num_count = 0 capture_lock = threading.Lock() last_photo_time = time.time() # 拍照函数 def capture_photo(): global photo_num_count with capture_lock: ret, frame = camera.read() if ret: photo_num_count += 1 photo_filename = f'{photo_path}photo_{photo_num_count}.jpg' cv2.imwrite(photo_filename, frame) print(f'{photo_num_count} photos saved. new photo: {photo_filename}') # 拍照请求处理 @app.route('/photo', methods=['POST']) def photo(): capture_photo() return jsonify({"status": "photo captured"}) # 停止请求处理 @app.route('/stop', methods=['POST']) def stop(): camera.release() print("Camera stopped") return jsonify({"status": "camera stopped"}) if __name__ == '__main__': app.run(host='0.0.0.0', port=5000)