Streaming: skip duplicate frames, 10fps

This commit is contained in:
Petr Kracik 2020-05-24 11:08:14 +02:00
parent 8145b34142
commit b1fb9a30c8

View File

@ -7,6 +7,7 @@ import datetime
import imutils
import time
import cv2
from hashlib import md5
outputFrame = None
lock = threading.Lock()
@ -15,9 +16,13 @@ lock = threading.Lock()
app = Flask(__name__)
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FPS, 10)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
fps = cap.get(cv2.CAP_PROP_FPS)
print("Frames per second using video.get(cv2.CAP_PROP_FPS) : {0}".format(fps))
time.sleep(1.0)
@app.route("/")
@ -28,16 +33,23 @@ def index():
def detect_motion(frameCount):
global vs, outputFrame, lock
lastframe = None
while True:
flag, frame = cap.read()
framedig = md5(frame).digest()
if lastframe == framedig:
print("Duplicate frame, skipping")
continue
if not flag:
continue
lastframe = framedig
timestamp = datetime.datetime.now()
cv2.putText(frame, timestamp.strftime(
"%A %d %B %Y %I:%M:%S%p"), (10, frame.shape[0] - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255), 1)
"%A %d %B %Y %I:%M:%S%p"), (10, 15),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 0), 1)
with lock:
outputFrame = frame.copy()
@ -58,6 +70,8 @@ def generate():
yield(b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' +
bytearray(encodedImage) + b'\r\n')
time.sleep(0.2)
@app.route("/video_feed")
def video_feed():