Spaces:
Runtime error
Runtime error
xcurvnubaim
commited on
Commit
·
f769dab
1
Parent(s):
19d1fa8
refactor: change cv2 to pillow to read image
Browse files
main.py
CHANGED
@@ -40,29 +40,22 @@ def classify_image(img):
|
|
40 |
return confidences
|
41 |
|
42 |
def animal_detect_and_classify(img_path):
|
43 |
-
# Read the image
|
44 |
-
img =
|
45 |
|
46 |
# Pass the image through the detection model and get the result
|
47 |
-
detect_results = detection_model(img)
|
48 |
|
49 |
combined_results = []
|
50 |
-
|
51 |
-
# Iterate over the detected objects
|
52 |
# Iterate over detections
|
53 |
for result in detect_results:
|
54 |
flag = False
|
55 |
for box in result.boxes:
|
56 |
flag = True
|
57 |
-
#
|
58 |
-
# Crop the RoI
|
59 |
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
60 |
-
detect_img = img
|
61 |
-
# Convert the image to RGB format
|
62 |
-
detect_img = cv2.cvtColor(detect_img, cv2.COLOR_BGR2RGB)
|
63 |
-
|
64 |
-
# Resize the input image to the expected shape (224, 224)
|
65 |
-
detect_img = cv2.resize(detect_img, (224, 224))
|
66 |
|
67 |
# Convert the image to a numpy array
|
68 |
inp_array = np.array(detect_img)
|
@@ -75,6 +68,7 @@ def animal_detect_and_classify(img_path):
|
|
75 |
|
76 |
# Make predictions using the classification model
|
77 |
prediction = classification_model.predict(inp_array)
|
|
|
78 |
# Map predictions to labels
|
79 |
threshold = 0.66
|
80 |
predicted_labels = [labels[np.argmax(pred)] if np.max(pred) >= threshold else "animal" for pred in prediction]
|
@@ -82,12 +76,10 @@ def animal_detect_and_classify(img_path):
|
|
82 |
combined_results.append(((x1, y1, x2, y2), predicted_labels))
|
83 |
if flag:
|
84 |
continue
|
85 |
-
|
86 |
-
|
87 |
-
detect_img =
|
88 |
-
|
89 |
-
inp_array = np.array(detect_img)
|
90 |
-
inp_array = inp_array.reshape((-1, 224, 224, 3))
|
91 |
inp_array = tf.keras.applications.efficientnet.preprocess_input(inp_array)
|
92 |
prediction = classification_model.predict(inp_array)
|
93 |
threshold = 0.66
|
|
|
40 |
return confidences
|
41 |
|
42 |
def animal_detect_and_classify(img_path):
|
43 |
+
# Read the image using Pillow
|
44 |
+
img = Image.open(img_path)
|
45 |
|
46 |
# Pass the image through the detection model and get the result
|
47 |
+
detect_results = detection_model(np.array(img))
|
48 |
|
49 |
combined_results = []
|
50 |
+
|
|
|
51 |
# Iterate over detections
|
52 |
for result in detect_results:
|
53 |
flag = False
|
54 |
for box in result.boxes:
|
55 |
flag = True
|
56 |
+
# Crop the Region of Interest (RoI)
|
|
|
57 |
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
58 |
+
detect_img = img.crop((x1, y1, x2, y2)).resize((224, 224))
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
# Convert the image to a numpy array
|
61 |
inp_array = np.array(detect_img)
|
|
|
68 |
|
69 |
# Make predictions using the classification model
|
70 |
prediction = classification_model.predict(inp_array)
|
71 |
+
|
72 |
# Map predictions to labels
|
73 |
threshold = 0.66
|
74 |
predicted_labels = [labels[np.argmax(pred)] if np.max(pred) >= threshold else "animal" for pred in prediction]
|
|
|
76 |
combined_results.append(((x1, y1, x2, y2), predicted_labels))
|
77 |
if flag:
|
78 |
continue
|
79 |
+
# If no detections found, consider the whole image
|
80 |
+
x2, y2 = img.size
|
81 |
+
detect_img = img.resize((224, 224))
|
82 |
+
inp_array = np.array(detect_img).reshape((-1, 224, 224, 3))
|
|
|
|
|
83 |
inp_array = tf.keras.applications.efficientnet.preprocess_input(inp_array)
|
84 |
prediction = classification_model.predict(inp_array)
|
85 |
threshold = 0.66
|