I have a trained Object Detection model that I have exported the code into VS code on Windows. I want to change the code so that I can input a number and the program finds and outputs all the photos in the dataset that contain the corresponding number. If anyone can help me with writing the code please leave a reply. I have tried using ChatGPT to alter the code but it hasn’t had complete success. Leave a reply if you need me to answer and specific question.
import os
from flask import Flask, render_template, request
from roboflow import Roboflow
app = Flask(name)
Set up Roboflow API
rf = Roboflow(api_key=“2PEpCGO4fvFma7yeGpP0”)
project = rf.workspace().project(“rider-number-finder”)
model = project.version(2).model
Function to perform object detection based on the user input
def perform_object_detection(number):
# Get a list of all image files in the input_images directory
input_images_dir = os.path.join(“static”, “input_images”)
image_files = [f for f in os.listdir(input_images_dir) if os.path.isfile(os.path.join(input_images_dir, f))]
# Perform object detection on each image and filter the results
filtered_images = []
for image_file in image_files:
input_image_path = os.path.join(input_images_dir, image_file)
predictions = model.predict(input_image_path, confidence=40, overlap=30)
# Check if the desired number is present in the predictions
for prediction in predictions:
for obj in prediction["objects"]:
if obj["class_name"] == "number" and obj["name"] == number:
filtered_images.append(image_file)
break # Found the number, no need to check further objects
# Save the filtered images to the predictions folder
output_images_dir = os.path.join("static", "predictions")
os.makedirs(output_images_dir, exist_ok=True)
for image_file in filtered_images:
input_image_path = os.path.join(input_images_dir, image_file)
output_image_path = os.path.join(output_images_dir, image_file)
os.rename(input_image_path, output_image_path)
return filtered_images
@app.route(‘/’, methods=[‘GET’, ‘POST’])
def home():
if request.method == ‘POST’:
number = request.form[‘number’]
# Perform object detection and get the list of filtered images
filtered_images = perform_object_detection(number)
return render_template('result.html', images=filtered_images)
return render_template('index.html', css_file='styles.css')
if name == ‘main’:
app.run()
KeyError: ‘objects’
When I tell chatgpt the error it says to change it to ‘predictions’ then I get the error in reverse.