I have one thread for constant processing of the image from the camera, and the second thread is the flash server. The frame from the first thread must be transferred to the server. But both threads do not start for me - either the first or the second.
#!/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()
It is important for me that the process of processing from the camera was at the start of the script, and not started when it was imaged to the server.
attempts to create a third thread did not help.
how I found it to fix it. It was necessary to move both tasks into separate threads:
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))