Spaces:
Runtime error
Runtime error
Update app.py
Browse files40 unmodified lines41424344454647484950515260 unmodified lines11311411511611711811912012112212312412512612712812913082 unmodified lines21321421521621721821930 unmodified lines25025125225325425525625725825926026126226326426540 unmodified lines# Load the fine-tuned YOLOv8n modeltry:yolo_model = YOLO('/content/yolo11n.pt') # Using the base model as an example# general_model = YOLO('/content/yolov8n_plus_fruitsnveggies.pt')phone_model = YOLO('/content/phone_model.pt')car_model = keras.saving.load_model(car_model_path)motorcycle_model = YOLO('/content/motorcycle_model.pt')fruitsnveggy_model = YOLO('/content/best_fruitveggy.pt')# Person.kerasethnicity_model = keras.saving.load_model(ethnicity_model_path)gender_model = keras.saving.load_model(gender_model_path)60 unmodified linesreturn img_arraydef car_pred(car_img_array):# 2. Make the Predictionpredictions = car_model.predict(img_array)prediction_scores = predictions[0]# 3. Decode the Prediction# We find the index with the highest score and use it to get the brand name.predicted_index = np.argmax(prediction_scores)predicted_class_name = car_class_names[predicted_index]confidence_score = 100 * np.max(prediction_scores)return predicted_class_name#print(f"Confidence: {confidence_score:.2f}%")def person_pred(person_img_array):# Preprocess the cropped person image82 unmodified lines# BOXINGx1, y1, x2, y2 = map(float, box.xyxy[0])x1, y1, x2, y2 = round(x1, 2), round(y1, 2), round(x2, 2), round(y2, 2)# CROPPING# Open the image file inside the loop for each detected objecti_image = Image.open(img_path)30 unmodified linesspecialized_captions_list.append(f'[{i+1}] {final_description(output_img_path, f'Describe the {class_name} smartphone, its color, and the details about it, with less than 15 words.')}')specialized_objects_list.append(class_name)elif cls == ('banana' or 'apple' or 'carrot' or 'broccoli'): #fruitnvegetables # YOLOresults = fruitsnveggy_model(cropped)for box in results[0].boxes:class_id = int(box.cls[0])class_name = fruitsnveggy_model.names[class_id] # Get the class name using the class IDspecialized_captions_list.append(f'[{i+1}] {final_description(output_img_path, f'Describe the vegetable/vegetables that are in the picture. What it is, how its presented, its details, with less than 15 words.')}')specialized_objects_list.append(class_name)else:specialized_captions_list.append(f'[{i+1}] {specialized_caption(cls, cropped)}')specialized_objects_list.append(cls)combined_captions = f"\n".join(specialized_captions_list) # list of llama(aided) specialized captions40 unmodified lines41424344454647484950515260 unmodified lines11311411511611711811912012112212312412512612712812913013113282 unmodified lines21521621721821922022122222322422522622722822923030 unmodified lines26126226326426526626726826927027127227327427527640 unmodified lines# Load the fine-tuned YOLOv8n modeltry:yolo_model = YOLO('yolo11n.pt') # Using the base model as an example# general_model = YOLO('/content/yolov8n_plus_fruitsnveggies.pt')phone_model = YOLO('phone_model.pt')car_model = keras.saving.load_model(car_model_path)motorcycle_model = YOLO('motorcycle_model.pt')fruitsnveggy_model = YOLO('best_fruitveggy.pt')# Person.kerasethnicity_model = keras.saving.load_model(ethnicity_model_path)gender_model = keras.saving.load_model(gender_model_path)60 unmodified linesreturn img_arraydef car_pred(car_img_array):# 2. Make the Predictionimg = car_img_array.resize((IMG_WIDTH, IMG_HEIGHT))img_array = tf.keras.utils.img_to_array(img)img_array = img_array / 255.0img_array = np.expand_dims(img_array, 0) # Create batch dimension# 3. Decode the Prediction# We find the index with the highest score and use it to get the brand name.predicted_index = np.argmax(prediction_scores)predicted_class_name = car_class_names[predicted_index]confidence_score = 100 * np.max(prediction_scores)return predicted_class_name#print(f"Confidence: {confidence_score:.2f}%")def person_pred(person_img_array):# Preprocess the cropped person image82 unmodified lines# BOXINGx1, y1, x2, y2 = map(float, box.xyxy[0])x1, y1, x2, y2 = round(x1, 2), round(y1, 2), round(x2, 2), round(y2, 2)detections.append({"label": cls,"confidence (%)": f"{conf}%","bbox": {"x1": x1,"y1": y1,"x2": x2,"y2": y2}})# CROPPING# Open the image file inside the loop for each detected objecti_image = Image.open(img_path)30 unmodified linesspecialized_captions_list.append(f'[{i+1}] {final_description(output_img_path, f'Describe the {class_name} smartphone, its color, and the details about it, with less than 15 words.')}')specialized_objects_list.append(class_name)elif cls in ('banana', 'apple', 'carrot', 'broccoli'): #fruitnvegetables # YOLOresults = fruitsnveggy_model(cropped)for box in results[0].boxes:class_id = int(box.cls[0])class_name = fruitsnveggy_model.names[class_id] # Get the class name using the class IDspecialized_captions_list.append(f'[{i+1}] {final_description(output_img_path, f'Describe the vegetable/vegetables or fruit/fruits that are in the picture. What it is, how its presented, its details, with less than 15 words.')}')specialized_objects_list.append(class_name)else:specialized_captions_list.append(f'[{i+1}] {final_description(output_img_path, f'Describe the object ({class_name}) in this picture. Describe any details about in in less than 15 words.')}')specialized_objects_list.append(cls)combined_captions = f"\n".join(specialized_captions_list) # list of llama(aided) specialized captions
app.py+27-16