Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,21 +4,45 @@ from ultralytics import YOLO
|
|
| 4 |
import requests
|
| 5 |
import json
|
| 6 |
import logging
|
|
|
|
| 7 |
|
| 8 |
logging.basicConfig(level=logging.INFO)
|
| 9 |
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
def detect_objects(images):
|
| 13 |
-
classes={
|
| 14 |
-
names = []
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
else:
|
| 23 |
names.append(['None'])
|
| 24 |
# print(names)
|
|
|
|
| 4 |
import requests
|
| 5 |
import json
|
| 6 |
import logging
|
| 7 |
+
import cv2
|
| 8 |
|
| 9 |
logging.basicConfig(level=logging.INFO)
|
| 10 |
|
| 11 |
+
model_detection = YOLO('/content/drive/MyDrive/Databae/ART/runs/train/yolov8_detection/weights/best.pt')
|
| 12 |
+
model_classification = YOLO('/content/drive/MyDrive/Databae/ART/runs_positive/train/yolov8_classification/weights/best.pt')
|
| 13 |
|
| 14 |
def detect_objects(images):
|
| 15 |
+
classes={ 2: "Positive", 1: "Negative"}
|
| 16 |
+
names = []
|
| 17 |
+
results_detection = model_detection(image_path)
|
| 18 |
+
|
| 19 |
+
# Load the image using OpenCV
|
| 20 |
+
# img = cv2.imread(image_path)
|
| 21 |
+
print(type(img))
|
| 22 |
+
# Process each detected object
|
| 23 |
+
for result in results_detection:
|
| 24 |
+
for box in result.boxes:
|
| 25 |
+
# Get bounding box coordinates (x1, y1, x2, y2)
|
| 26 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
| 27 |
+
|
| 28 |
+
# Crop the bounding box from the image
|
| 29 |
+
cropped_img = img[y1:y2, x1:x2]
|
| 30 |
+
|
| 31 |
+
# Resize the cropped image to 640x640
|
| 32 |
+
resized_img = cv2.resize(cropped_img, (640, 640))
|
| 33 |
+
resized_img = cv2.cvtColor(resized_img, cv2.COLOR_BGR2RGB)
|
| 34 |
+
# Perform inference using the classification model
|
| 35 |
+
results_classification = model_classification.predict(resized_img,save = True)
|
| 36 |
+
|
| 37 |
+
detected = False
|
| 38 |
+
print(results_classification)
|
| 39 |
+
# Process classification results
|
| 40 |
+
for res in results_classification:
|
| 41 |
+
# Get class probabilities and labels
|
| 42 |
+
top1_class = res.probs.top1 # Predicted class
|
| 43 |
+
top1_confidence = res.probs.top1conf
|
| 44 |
+
print(top1_class)
|
| 45 |
+
name.append([classes[top1_class]])
|
| 46 |
else:
|
| 47 |
names.append(['None'])
|
| 48 |
# print(names)
|