Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please help me , when I record a video it freezes. I will attach my code below import cv 2 from flask import Flask, Response,
Please help me when I record a video it freezes. I will attach my code below
import cv
from flask import Flask, Response, request, redirect, rendertemplate, urlfor
from ultralytics import YOLO
import supervision as sv
from datetime import datetime
import os
import numpy as np
from werkzeug.utils import securefilename
from collections import deque
from scipy.spatial import distance as dist
import matplotlib.pyplot as plt
import io
UPLOADFOLDER 'staticvideos
temppath
stream False
frame npzeros npuint
app Flaskname
app.secretkey 'akwdhawwdjhawkdh'
app.configUPLOADFOLDER' UPLOADFOLDER
# Initialize the custom YOLOv model
weights 'modelsyolovspt # Path to custom YOLOv weights file
model YOLOweights
ALLOWEDEXTENSIONS setmpmkv
totalcars
carsnorth
carssouth
carcentroids dequemaxlen
@app.route methodsGET 'POST'
def index:
global stream, temppath
if request.method "POST":
if request.form.getsubmit "Upload Video":
file request.filesfile
if file.filename :
return redirect
if file and allowedfilefilefilename:
filename securefilenamestrdatetimenow file.filename
temppath ospath.joinappconfigUPLOADFOLDER' filename
file.savetemppath
stream True
return rendertemplateindexhtml
if request.form.getsubmit "Start Recording":
temppath
stream True
return redirect
if request.form.getsubmit "Stop Recording" and stream True:
stream False
return redirect
return rendertemplateindexhtml totalcarstotalcars, carsnorthcarsnorth, carssouthcarssouth
def allowedfilefilename:
return in filename and filename.rsplitlower in ALLOWEDEXTENSIONS
def detectobjectsframe:
results modelframe verboseFalse
frame results.plot
return results, frame
def genframes:
global totalcars, carsnorth, carssouth, carcentroids, temppath
if stream:
if temppath :
cap cvVideoCapture
else:
cap cvVideoCapturetemppath
while True:
success, frame cap.read
if not success:
break
if not stream:
break
else:
results, frame detectobjectsframe
currentcentroids
for result in results.boxes:
x y x y mapint result.xyxy
centroid intx x inty y
currentcentroids.appendcentroid
if lencarcentroids:
D dist.cdistcarcentroids, currentcentroids
rows Dminaxisargsort
cols Dargminaxisrows
usedrows, usedcols set set
for row col in ziprows cols:
if row in usedrows or col in usedcols:
continue
carcentroidsrow currentcentroidscol
usedrows.addrow
usedcols.addcol
x y carcentroidsrow
if y frame.shape:
carsnorth
else:
carssouth
totalcars
unusedrows setrange Dshapedifferenceusedrows
unusedcols setrange Dshapedifferenceusedcols
for row in unusedrows:
carcentroidsrow None
for col in unusedcols:
carcentroids.appendcurrentcentroidscol
else:
carcentroids currentcentroids
ret, buffer cvimencodejpg frame
frame buffer.tobytes
yield bframer
b'ContentType: imagejpegr
r
frame br
@app.routevideofeed'
def videofeed:
return Responsegenframes mimetype'multipartxmixedreplace; boundaryframe'
if namemain:
app.runport debugTrue
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started