diff --git a/carsrun/appCam.py b/carsrun/appCam.py index da41158..0e85ee3 100644 --- a/carsrun/appCam.py +++ b/carsrun/appCam.py @@ -19,18 +19,31 @@ from car import CAR #from PiCamera_H264_Server import stream import threading import cv2 +import requests 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 +55,79 @@ 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('/control/sendit', methods=['POST']) +def send_command(): + # 定义目标 IP 地址、端口和路由 + ip = "192.168.98.204" + port = 3000 + route = "sendit2" + + # 从请求中获取消息内容 + data = request.json + message = data.get('message') + + if message is None: + return jsonify({"error": "缺少 message 参数"}), 400 + + # 构建完整的 URL + url = f"http://{ip}:{port}/{route}" + + # 发送 POST 请求 + try: + response = requests.post(url, json={'message': message}) + response.raise_for_status() # 检查请求是否成功 + return f"命令已发送,服务器响应: {response.text}" + except requests.exceptions.RequestException as e: + return f"请求失败: {str(e)}", 500 + +@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..8a987bd 100644 --- a/carsrun/templates/index-t.html +++ b/carsrun/templates/index-t.html @@ -25,9 +25,11 @@
小车控制界面