Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!