Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me to fix this problem about my code. The problem is the counting object, thats the part of the problem please help me

Please help me to fix this problem about my code. The problem is the counting object, thats the part of the problem please help me out. Thank you
heres the code:
from flask import Flask, render_template, request, url_for
from werkzeug.utils import secure_filename
import os
from ultralytics import YOLO
import cv2
import uuid
app = Flask(__name__)
app.config['UPLOAD_FOLDER']= 'static/uploads'
@app.route('/')
def index():
return render_template('uploadindex.html')
@app.route('/', methods=['POST'])
def upload_file():
if 'file' not in request.files:
return 'No file part'
file = request.files['file']
if file.filename =='':
return 'No selected file'
if file:
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
unique_output_filename = 'uploads/'+ str(uuid.uuid4())+'_processed.mp4'
class_counts = process_video(os.path.join(app.config['UPLOAD_FOLDER'], filename), unique_output_filename) # Capture the class counts
return render_template('video.html', filename=unique_output_filename, class_counts=class_counts)
def process_video(video_path, output_path):
model = YOLO('test6.pt')
cap = cv2.VideoCapture(video_path)
fps = cap.get(cv2.CAP_PROP_FPS)
fourcc = cv2.VideoWriter_fourcc(*'avc1') # use 'XVID' codec
out = cv2.VideoWriter('static/'+ output_path, fourcc, fps,(640,480))
ret = True
class1_count =0 # Initialize class 1 count
class2_count =0 # Initialize class 2 count
while ret:
ret, frame = cap.read()
if ret:
frame = cv2.resize(frame,(640,480)) # resize the frame
results = model.track(frame, persist=True, tracker="bytetrack.yaml")[0]
# Count objects for each class
for result in results.boxes.data:
class_id = int(result[5]) # Assuming the class ID is at index 5
if class_id ==0: # Replace 0 with the class ID for class 1
class1_count +=1
elif class_id ==1: # Replace 1 with the class ID for class 2
class2_count +=1
frame_= results.plot()
out.write(frame_)
cap.release()
out.release()
cv2.destroyAllWindows()
return {'class1': class1_count, 'class2': class2_count} # Return the counts of each class
if __name__=='__main__':
app.run(debug=True)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

what is the subnet mask of CIDR notation / 1 9 ?

Answered: 1 week ago

Question

8. Explain the relationship between communication and context.

Answered: 1 week ago

Question

d. How were you expected to contribute to family life?

Answered: 1 week ago