0% found this document useful (0 votes)
11 views

Text 4

Uploaded by

monkefist77
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Text 4

Uploaded by

monkefist77
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import time

import threading
from flask import Flask, render_template, Response
import cv2

class CameraBufferCleanerThread(threading.Thread):
def __init__(self, camera, name='camera-buffer-cleaner-thread'):
self.camera = camera
self.incoming = False
self.last_frame = None
super(CameraBufferCleanerThread, self).__init__(name=name)
self.daemon = True
self.start()

def run(self):
while True:
ret, self.last_frame = self.camera.read()
if ret:
self.incoming = True

app = Flask(__name__)

cap = cv2.VideoCapture('udpsrc port=5200 ! application/x-rtp, encoding-


name=JPEG,payload=26 ! rtpjpegdepay ! jpegdec ! video/x-raw ! videoconvert !
video/x-raw,format=BGR ! appsink drop=1', cv2.CAP_GSTREAMER)
# cap = cv2.VideoCapture(0, cv2.CAP_V4L2)

# cap = cv2.VideoCapture("udpsrc port=4000 ! application/x-rtp, encoding-


name=(string)H264 ! rtph264depay ! avdec_h264 ! videoconvert ! video/x-
raw,format=BGR ! appsink drop=1", cv2.CAP_GSTREAMER)

video = CameraBufferCleanerThread(cap)

"""
def agen_frames():
while True:
success, frame = cap.read() # read the camera frame
if not success:
break
else:
cv2.imshow('frame', frame)
cv2.waitKey(1)
ret, buffer = cv2.imencode('.jpg', frame)
frame = buffer.tobytes()
time.sleep(0.5)
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') # concat
frame one by one and show result
"""

def gen_frames_alt():
while True:
if video.incoming:
frame = video.last_frame
cv2.imshow('frame', frame)
ret, buffer = cv2.imencode('.jpg', frame)
frame = buffer.tobytes()
time.sleep(0.5)
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')

def gen_frames():
while True:
if video.incoming:
frame = video.last_frame
cv2.imshow('frame', frame)
ret, buffer = cv2.imencode('.jpg', frame)
frame = buffer.tobytes()
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.html')

@app.route('/video_feed')
def video_feed():
return Response(gen_frames(), mimetype='multipart/x-mixed-replace;
boundary=frame')

@app.route('/video_feed_alt')
def video_feed_alt():
return Response(gen_frames_alt(), mimetype='multipart/x-mixed-replace;
boundary=frame')

app.run(host='0.0.0.0', port=8080, debug=True)

You might also like