From 70c4cfbed43bd6acbf096572aec4b25536c81afc Mon Sep 17 00:00:00 2001 From: zyp <1721777650@qq.com> Date: Wed, 30 Oct 2024 20:17:01 +0800 Subject: [PATCH 1/2] capture picture finish --- carsrun/appCam.py | 79 ++++++++++++++++-------- carsrun/templates/index-t.html | 109 +++++++++++++++++++++++++-------- 2 files changed, 136 insertions(+), 52 deletions(-) diff --git a/carsrun/appCam.py b/carsrun/appCam.py index da41158..134eb93 100644 --- a/carsrun/appCam.py +++ b/carsrun/appCam.py @@ -23,14 +23,26 @@ app = Flask(__name__, static_url_path='') # 照片保存路径 PHOTO_PATH = "photo.jpg" +picam2 = None # picam2 = Picamera2() # picam2.configure(picam2.create_video_configuration(main={"format": 'XRGB8888', "size": (640, 480)})) # picam2.start() -def gen_frames(): # generate frame by frame from camera +def cameraon(): + global picam2 picam2 = Picamera2() picam2.configure(picam2.create_video_configuration(main={"format": 'XRGB8888', "size": (640, 480)})) picam2.start() + + +def cameradown(): + global picam2 + picam2.stop() + picam2.close() + +def gen_frames(): # generate frame by frame from camera + global picam2 + cameraon() while True: # Capture frame-by-frame frame = picam2.capture_array() # read the camera frame @@ -42,39 +54,54 @@ def gen_frames(): # generate frame by frame from camera -def capture_frame(): - picam2.close() - frame = picam2.capture_array() # 假设picam2是你的相机对象 - ret, buffer = cv2.imencode('.jpg', frame, [int(cv2.IMWRITE_JPEG_QUALITY), 50]) - img_str = base64.b64encode(buffer).decode() - picam2.start() - return img_str +# def capture_frame(): +# picam2.close() +# frame = picam2.capture_array() # 假设picam2是你的相机对象 +# ret, buffer = cv2.imencode('.jpg', frame, [int(cv2.IMWRITE_JPEG_QUALITY), 50]) +# img_str = base64.b64encode(buffer).decode() +# picam2.start() +# return img_str + +# @app.route('/capture_frame') +# def capture_frame_route(): +# img_str = capture_frame() +# return jsonify(img_str) +@app.route('/capture') +def capture_image(): + global picam2 + # 获取一帧 + frame = picam2.capture_array() # 读取当前帧 + frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) + + # 定义保存图片的路径 + photo_path = "captured_photo.jpg" -@app.route('/capture_frame') -def capture_frame_route(): - img_str = capture_frame() - return jsonify(img_str) + # 将帧保存为图片 + try: + cv2.imwrite(photo_path, frame) + return f"Photo saved at {photo_path}" + except Exception as e: + return f"Failed to save photo: {str(e)}" @app.route('/') def index(): return render_template('index-t.html') -@app.route('/video_feed') -def video_feed(): - #Video streaming route. Put this in the src attribute of an img tag +# @app.route('/video_feed') +# def video_feed(): +# #Video streaming route. Put this in the src attribute of an img tag +# return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame') + +@app.route('/startvadio') +def startvadio(): return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame') -#--------------------------------------------------------------------------------- -@app.route('/take_photo') -def take_photo(): - picam2 = Picamera2() - picam2.configure(picam2.create_video_configuration(main={"format": 'XRGB8888', "size": (640, 480)})) - picam2.start() - frame = picam2.capture_array() # Capture a single frame - frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) - cv2.imwrite(PHOTO_PATH, frame) # Save the frame as a JPEG file - return send_file(PHOTO_PATH, as_attachment=True) # Send the file to download -#--------------------------------------------------------------------------------------- + +@app.route('/stopvadio') +def stopvadio(): + cameradown() + return "closed" + # def gen(camera): # """Video streaming generator function.""" diff --git a/carsrun/templates/index-t.html b/carsrun/templates/index-t.html index 2c6c302..80f625a 100644 --- a/carsrun/templates/index-t.html +++ b/carsrun/templates/index-t.html @@ -25,9 +25,11 @@
小车控制界面