我有一个线程用于不断处理来自相机的图像,第二个线程是闪存服务器。来自第一个线程的帧必须传输到服务器。但两个线程都不会为我启动 - 无论是第一个还是第二个。
#!/usr/bin/env python
from datetime import datetime
from flask import Flask, render_template, Response, request
import numpy as np
import cv2
from threading import Thread
# Initialize the Flask app
app = Flask(__name__)
# global pwm_az pwm_tilt
pwm_az = 0
pwm_tilt = 0
global frame
frame = 0
def vd():
global frame
# define a video capture object
vid = cv2.VideoCapture(0)
print("Запущен поток с камерой")
th = Thread(target=webWork(),args=())
th.start()
th.join()
while (True):
# Capture the video frame
# by frame
ret, frame = vid.read()
# Display the resulting frame
cv2.imshow('frame', frame)
# the 'q' button is set as the
# quitting button you may use any
# desired button of your choice
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# After the loop release the cap object
vid.release()
# Destroy all the windows
cv2.destroyAllWindows()
def gen_frames():
global frame
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
@app.route('/')
def index():
return render_template('index_2.html')
@app.route('/video_feed')
def video_feed():
return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
def webWork():
if __name__ == "__main__":
app.run(host="localhost", port=8086, debug=True)
thread_videoWork = Thread(target=vd(), args=())
thread_webWork = Thread(target=webWork(), args=())
thread_webWork.start()
thread_videoWork.start()
对我来说很重要的是,从相机进行处理的过程是在脚本开始时进行的,而不是在将其成像到服务器时开始的。
尝试创建第三个线程没有帮助。
我如何找到它来修复它。有必要将这两个任务移至单独的线程中:
if __name__ == "__main__":
try:
print(f'start first thread')
t1 = Thread(target=runApp).start()
print(f'start second thread')
t2 = Thread(target=videoWork).start()
except Exception as e:
print("Unexpected error:" + str(e))