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