Hi Folks,
i am working on web app to control material on correct positions. All cameras are on Rpi zeros where running UV4L streaming server (uv4l working like charm, stream works many days without any issues). Flask runs on win server (no any problem with another flask apps). When run these cameras on flask webpage using CV2 then all working fine but just some time, like 1-2 mins then all streams are stopped and needs to refresh page. So now i am not sure if its problem of flask or cv2. thx for any help
BR
Antra
main.py
RpiIpsList = GetRpiIps()
@app.route("/cameras", methods=["GET"])
def cameras():
return render_template('cameras.html',camera_list=len(RpiIpsList), RpiIpsList=RpiIpsList)
@app.route('/video_feed/<string:campera_ip>/', methods=["GET"])
def video_feed(campera_ip):
#Video streaming route. Put this in the src attribute of an img tag
Status=RpiAvailability(campera_ip)
if Status==1:
return Response(gen_frames(campera_ip), mimetype='multipart/x-mixed-replace; boundary=frame')
else:
return "Camera not avaliable"+ campera_ip
sql.py
import pymssql
server = "****"
user = "****"
password = "****"
DB = "****"
def GetRpiIps():
conn = pymssql.connect(server, user, password, DB)
cur = conn.cursor()
cur.execute("select RpiName,RpiIP,Udt from [OPENCV].[dbo].[RPI_CamList]")
results = cur.fetchall()
conn.close
return results
cameras.py
import cv2
import socket
def RpiAvailability(Ip):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(0.5)
result = sock.connect_ex((Ip, 8080))
if result == 0:
return(1)
else:
return(0)
def gen_frames(Ip): # generate frame by frame from camera
camera = cv2.VideoCapture('http://'+ Ip +':8080/stream/video.mjpeg')
camera.set(cv2.CAP_PROP_BUFFERSIZE, 0)
while True:
success, image = camera.read()
ret, jpeg = cv2.imencode('.jpg', image)
frame = jpeg.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
cameras.html
<ol>
{% for camera_number in range(0, camera_list) %}
<li>
{{RpiIpsList[camera_number][0]}} - {{RpiIpsList[camera_number][1]}}, last connection {{RpiIpsList[camera_number][2]}}<br />
<img src="{{ url_for('video_feed', campera_ip=RpiIpsList[camera_number][1]) }}"><br />
</li>
{% endfor %}
</ol>