Ashu1803 commited on
Commit
14a7eac
1 Parent(s): 6884c4a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -38
app.py CHANGED
@@ -140,51 +140,75 @@ def match_and_identify(features, bbox):
140
  return identity, color
141
 
142
  def process_image(image):
143
- # Prepare the image tensor
144
- image_np = np.array(image)
145
- input_tensor = np.expand_dims(image_np, axis=0)
146
-
147
- # Run inference
148
- detections = detect_objects(input_tensor)
149
-
150
- # Extract output tensors and convert to numpy arrays
151
- boxes = detections[0].numpy()[0]
152
- scores = detections[1].numpy()[0]
153
- classes = detections[2].numpy()[0]
154
- num_detections = int(detections[3].numpy()[0])
155
-
156
- # Filter detections for 'person' class
157
- threshold = 0.3 # Adjust this threshold as needed
158
- for i in range(num_detections):
159
- class_id = int(classes[i])
160
- score = scores[i]
161
- box = boxes[i]
162
 
163
- if class_id == 1 and score > threshold:
164
- h, w, _ = image.shape
165
- ymin, xmin, ymax, xmax = box
166
- left, right, top, bottom = int(xmin * w), int(xmax * w), int(ymin * h), int(ymax * h)
167
-
168
- # Extract person ROI
169
- person_roi = image[top:bottom, left:right]
170
 
171
- # Extract features
172
- features = extract_features(person_roi)
 
173
 
174
- # Predict bbox using Kalman filter
175
- predicted_bbox = np.array([xmin, ymin, xmax, ymax])
176
-
177
- # Match and identify
178
- identity, color = match_and_identify(features, predicted_bbox)
179
 
180
- # Draw bounding box
181
- left, top, right, bottom = int(predicted_bbox[0] * w), int(predicted_bbox[1] * h), int(predicted_bbox[2] * w), int(predicted_bbox[3] * h)
182
- cv2.rectangle(image, (left, top), (right, bottom), color, 2)
183
- cv2.putText(image, f'Person {identity}', (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184
 
185
  return image
186
-
187
  def gradio_interface(input_image):
 
 
 
 
 
 
 
188
  # Process the input image
189
  output_image = process_image(input_image)
190
  return output_image
 
140
  return identity, color
141
 
142
  def process_image(image):
143
+ if image is None:
144
+ return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
 
146
+ # Convert image to RGB if it's not
147
+ if len(image.shape) == 2: # Grayscale
148
+ image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
149
+ elif image.shape[2] == 4: # RGBA
150
+ image = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)
 
 
151
 
152
+ # Ensure image is uint8
153
+ if image.dtype != np.uint8:
154
+ image = (image * 255).astype(np.uint8)
155
 
156
+ # Prepare the image tensor
157
+ image_np = np.array(image)
158
+ input_tensor = np.expand_dims(image_np, axis=0)
 
 
159
 
160
+ try:
161
+ # Run inference
162
+ detections = detect_objects(input_tensor)
163
+
164
+ # Extract output tensors and convert to numpy arrays
165
+ boxes = detections[0].numpy()[0]
166
+ scores = detections[1].numpy()[0]
167
+ classes = detections[2].numpy()[0]
168
+ num_detections = int(detections[3].numpy()[0])
169
+
170
+ # Filter detections for 'person' class
171
+ threshold = 0.3 # Adjust this threshold as needed
172
+ for i in range(num_detections):
173
+ class_id = int(classes[i])
174
+ score = scores[i]
175
+ box = boxes[i]
176
+
177
+ if class_id == 1 and score > threshold:
178
+ h, w, _ = image.shape
179
+ ymin, xmin, ymax, xmax = box
180
+ left, right, top, bottom = int(xmin * w), int(xmax * w), int(ymin * h), int(ymax * h)
181
+
182
+ # Extract person ROI
183
+ person_roi = image[top:bottom, left:right]
184
+
185
+ # Extract features
186
+ features = extract_features(person_roi)
187
+
188
+ # Predict bbox using Kalman filter
189
+ predicted_bbox = np.array([xmin, ymin, xmax, ymax])
190
+
191
+ # Match and identify
192
+ identity, color = match_and_identify(features, predicted_bbox)
193
+
194
+ # Draw bounding box
195
+ left, top, right, bottom = int(predicted_bbox[0] * w), int(predicted_bbox[1] * h), int(predicted_bbox[2] * w), int(predicted_bbox[3] * h)
196
+ cv2.rectangle(image, (left, top), (right, bottom), color, 2)
197
+ cv2.putText(image, f'Person {identity}', (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
198
+
199
+ except Exception as e:
200
+ print(f"Error during processing: {str(e)}")
201
+ return image # Return original image if there's an error
202
 
203
  return image
 
204
  def gradio_interface(input_image):
205
+ if input_image is None:
206
+ return None
207
+
208
+ # Convert PIL Image to numpy array if necessary
209
+ if hasattr(input_image, 'convert'):
210
+ input_image = np.array(input_image.convert('RGB'))
211
+
212
  # Process the input image
213
  output_image = process_image(input_image)
214
  return output_image