xcurvnubaim commited on
Commit
1e0fed5
1 Parent(s): 892d864

feat: add object detection

Browse files
Files changed (2) hide show
  1. app.py +108 -4
  2. requirements.txt +3 -1
app.py CHANGED
@@ -3,9 +3,14 @@ import gradio as gr
3
  import tensorflow as tf
4
  from io import StringIO
5
  from PIL import Image
 
 
 
6
 
7
  labels = []
8
- model = tf.keras.models.load_model('./models.h5')
 
 
9
  with open("labels.txt") as f:
10
  for line in f:
11
  labels.append(line.replace('\n', ''))
@@ -19,10 +24,109 @@ def classify_image(inp):
19
  inp_copy = np.array(inp_copy)
20
  inp_copy = inp_copy.reshape((-1, 224, 224, 3))
21
  inp_copy = tf.keras.applications.efficientnet.preprocess_input(inp_copy)
22
- prediction = model.predict(inp_copy).flatten()
23
  confidences = {labels[i]: float(prediction[i]) for i in range(90)}
24
  return confidences
25
 
26
- demo = gr.Interface(classify_image, gr.Image(), gr.Label(num_top_classes=3))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  if __name__ == "__main__":
28
- demo.launch()
 
 
 
3
  import tensorflow as tf
4
  from io import StringIO
5
  from PIL import Image
6
+ from ultralytics import YOLO
7
+ import cv2
8
+ from datetime import datetime
9
 
10
  labels = []
11
+ classification_model = tf.keras.models.load_model('./models.h5')
12
+ detection_model = YOLO('./best.pt')
13
+
14
  with open("labels.txt") as f:
15
  for line in f:
16
  labels.append(line.replace('\n', ''))
 
24
  inp_copy = np.array(inp_copy)
25
  inp_copy = inp_copy.reshape((-1, 224, 224, 3))
26
  inp_copy = tf.keras.applications.efficientnet.preprocess_input(inp_copy)
27
+ prediction = classification_model.predict(inp_copy).flatten()
28
  confidences = {labels[i]: float(prediction[i]) for i in range(90)}
29
  return confidences
30
 
31
+ def animal_detect_and_classify(img, detect_results):
32
+ img = np.array(img)
33
+ combined_results = []
34
+ # Iterate over detections
35
+ for result in detect_results:
36
+ for box in result.boxes:
37
+ # print(box)
38
+ # Crop the RoI
39
+ x1, y1, x2, y2 = map(int, box.xyxy[0])
40
+ detect_img = img[y1:y2, x1:x2]
41
+ # Convert the image to RGB format
42
+ # detect_img = cv2.cvtColor(detect_img, cv2.COLOR_BGR2RGB)
43
+
44
+ # Resize the input image to the expected shape (224, 224)
45
+ detect_img = cv2.resize(detect_img, (224, 224))
46
+
47
+ # Convert the image to a numpy array
48
+ inp_array = np.array(detect_img)
49
+
50
+ # Reshape the array to match the expected input shape
51
+ inp_array = inp_array.reshape((-1, 224, 224, 3))
52
+
53
+ # Preprocess the input array
54
+ inp_array = tf.keras.applications.efficientnet.preprocess_input(inp_array)
55
+
56
+ # Make predictions using the classification model
57
+ prediction = classification_model.predict(inp_array)
58
+ # Map predictions to labels
59
+ threshold = 0.66
60
+ confidences_classification = {labels[i]: float(prediction[0][i]) for i in range(90)}
61
+ print(confidences_classification)
62
+ predicted_labels = [labels[np.argmax(pred)] if np.max(pred) >= threshold else "animal" for pred in prediction]
63
+ combined_results.append(((x1, y1, x2, y2), predicted_labels))
64
+ return combined_results
65
+
66
+ def generate_color(class_name):
67
+ # Generate a hash from the class name
68
+ color_hash = hash(class_name)
69
+ # Normalize the hash value to fit within the range of valid color values (0-255)
70
+ color_hash = abs(color_hash) % 16777216
71
+ R = color_hash//(256*256)
72
+ G = (color_hash//256) % 256
73
+ B = color_hash % 256
74
+ # Convert the hash value to RGB color format
75
+ color = (R, G, B)
76
+
77
+ return color
78
+
79
+ def plot_detected_rectangles(image, detections):
80
+ # Create a copy of the image to draw on
81
+ image = np.array(image)
82
+ img_with_rectangles = image.copy()
83
+
84
+ # Iterate over each detected rectangle and its corresponding class name
85
+ for rectangle, class_names in detections:
86
+ if class_names[0] == "unknown":
87
+ continue
88
+ # Extract the coordinates of the rectangle
89
+ x1, y1, x2, y2 = rectangle
90
+
91
+ # Generate a random color
92
+ color = generate_color(class_names[0])
93
+
94
+ # Draw the rectangle on the image
95
+ cv2.rectangle(img_with_rectangles, (x1, y1), (x2, y2), color, 2)
96
+
97
+ # Put the class names above the rectangle
98
+ for i, class_name in enumerate(class_names):
99
+ cv2.putText(img_with_rectangles, class_name, (x1, y1 - 10 - i*20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
100
+
101
+ return img_with_rectangles
102
+
103
+ def detection_image(img, conf_threshold, iou_threshold):
104
+ results = detection_model.predict(
105
+ source=img,
106
+ conf=conf_threshold,
107
+ iou=iou_threshold,
108
+ show_labels=True,
109
+ show_conf=True,
110
+ imgsz=640,
111
+ )
112
+ combined_results = animal_detect_and_classify(img, results)
113
+ plotted_image = plot_detected_rectangles(img, combined_results)
114
+ return Image.fromarray(plotted_image)
115
+
116
+ io1 = gr.Interface(classify_image, gr.Image(), gr.Label(num_top_classes=3))
117
+ io2 = gr.Interface(
118
+ fn=detection_image,
119
+ inputs=[
120
+ gr.Image(type="pil", label="Upload Image"),
121
+ gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold"),
122
+ gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold")
123
+ ],
124
+ outputs=gr.Image(type="pil", label="Result"),
125
+ title="Animal Detection",
126
+ description="Upload images for inference. The Ultralytics YOLOv8n model is used as pretrained model",
127
+ )
128
+
129
  if __name__ == "__main__":
130
+ gr.TabbedInterface(
131
+ [io1, io2], ["Classification", "Object Detection"]
132
+ ).launch(debug=True)
requirements.txt CHANGED
@@ -9,4 +9,6 @@ gradio==3
9
  gradio_client==0.16.0
10
  numpy==1.25.2
11
  Pillow==9.4.0
12
- keras==2.15.0
 
 
 
9
  gradio_client==0.16.0
10
  numpy==1.25.2
11
  Pillow==9.4.0
12
+ keras==2.15.0
13
+ ultralytics
14
+ opencv-python-headless