Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import requests
|
| 4 |
+
import pickle
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
# Function to predict and show bounding boxes
|
| 8 |
+
def predict_and_show_bounding_boxes(image_path):
|
| 9 |
+
try:
|
| 10 |
+
# Load the image using cv2
|
| 11 |
+
img = cv2.imread(image_path)
|
| 12 |
+
if img is None:
|
| 13 |
+
print(f"Error: Could not load image at {image_path}")
|
| 14 |
+
return None, "Error: Could not load image"
|
| 15 |
+
|
| 16 |
+
# Perform inference using the loaded YOLO model (assuming 'model' is loaded)
|
| 17 |
+
results = model.predict(source=image_path, save=False, conf=0.5)
|
| 18 |
+
result = results[0]
|
| 19 |
+
boxes = result.boxes
|
| 20 |
+
|
| 21 |
+
if len(boxes) == 0:
|
| 22 |
+
# No defects found, show the zero defects image
|
| 23 |
+
zero_defects_img = cv2.imread('zero_defects.png') # Assuming 'zero_defects.png' is in the same directory
|
| 24 |
+
if zero_defects_img is not None:
|
| 25 |
+
img = zero_defects_img
|
| 26 |
+
return img
|
| 27 |
+
else:
|
| 28 |
+
return None, "Error: Could not load zero defects image"
|
| 29 |
+
|
| 30 |
+
for box in boxes:
|
| 31 |
+
xyxy = box.xyxy[0].tolist()
|
| 32 |
+
x_min, y_min, x_max, y_max = int(xyxy[0]), int(xyxy[1]), int(xyxy[2]), int(xyxy[3])
|
| 33 |
+
conf = box.conf[0].item()
|
| 34 |
+
cls = int(box.cls[0])
|
| 35 |
+
cv2.rectangle(img, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
|
| 36 |
+
label = f"{result.names[cls]}: {conf:.2f}"
|
| 37 |
+
cv2.putText(img, label, (x_min, y_min - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
|
| 38 |
+
|
| 39 |
+
# Return the processed image
|
| 40 |
+
return img
|
| 41 |
+
|
| 42 |
+
except Exception as e:
|
| 43 |
+
print(f"An error occurred during prediction: {e}")
|
| 44 |
+
return None, str(e)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
try:
|
| 48 |
+
with open('yolo_modelnano.pkl', 'rb') as file: # Assuming 'yolo_modelnano.pkl' is in the same directory
|
| 49 |
+
model = pickle.load(file)
|
| 50 |
+
print("YOLO model loaded successfully.")
|
| 51 |
+
except FileNotFoundError:
|
| 52 |
+
print("Error: 'yolo_modelnano.pkl' not found.")
|
| 53 |
+
except Exception as e:
|
| 54 |
+
print(f"An error occurred while loading the model: {e}")
|
| 55 |
+
|
| 56 |
+
# Create Gradio interface
|
| 57 |
+
iface = gr.Interface(
|
| 58 |
+
fn=predict_and_show_bounding_boxes,
|
| 59 |
+
inputs=gr.Image(type="filepath"),
|
| 60 |
+
outputs=[gr.Image()],
|
| 61 |
+
title="Defect Detection",
|
| 62 |
+
description="Upload an image to detect defects"
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
# Launch the interface with appropriate settings for PythonAnywhere
|
| 66 |
+
iface.launch(server_name="0.0.0.0", server_port=8080) # Example settings, adjust as needed
|