Saad0KH commited on
Commit
5f2adbf
·
verified ·
1 Parent(s): 1a75096

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -19
app.py CHANGED
@@ -40,42 +40,36 @@ def detect_person(
40
  vbboxes[:, 3] = kpss[:, 4, 1]
41
  return bboxes, vbboxes
42
 
43
- def visualize(image: np.ndarray, bboxes: np.ndarray, vbboxes: np.ndarray) -> np.ndarray:
44
- res = image.copy()
45
  for i in range(bboxes.shape[0]):
46
  bbox = bboxes[i]
47
- vbbox = vbboxes[i]
48
  x1, y1, x2, y2 = bbox
49
- vx1, vy1, vx2, vy2 = vbbox
50
- cv2.rectangle(res, (x1, y1), (x2, y2), (0, 255, 0), 1)
51
- alpha = 0.8
52
- color = (255, 0, 0)
53
- for c in range(3):
54
- res[vy1:vy2, vx1:vx2, c] = res[vy1:vy2, vx1:vx2, c] * alpha + color[c] * (1.0 - alpha)
55
- cv2.circle(res, (vx1, vy1), 1, color, 2)
56
- cv2.circle(res, (vx1, vy2), 1, color, 2)
57
- cv2.circle(res, (vx2, vy1), 1, color, 2)
58
- cv2.circle(res, (vx2, vy2), 1, color, 2)
59
- return res
60
 
61
  detector = load_model()
62
  detector.prepare(-1, nms_thresh=0.5, input_size=(640, 640))
63
 
64
- def detect(image: np.ndarray) -> np.ndarray:
65
  if image is None:
66
- return np.array([]) # Retourne une image vide si aucune image n'est fournie
67
 
68
  image = image[:, :, ::-1] # RGB -> BGR
69
  bboxes, vbboxes = detect_person(image, detector)
70
- res = visualize(image, bboxes, vbboxes)
71
- return res[:, :, ::-1] # BGR -> RGB
72
 
73
  examples = sorted(pathlib.Path("images").glob("*.jpg"))
74
 
75
  demo = gr.Interface(
76
  fn=detect,
77
  inputs=gr.Image(label="Input", type="numpy"),
78
- outputs=gr.Image(label="Output"),
79
  examples=examples,
80
  examples_per_page=30,
81
  title=TITLE,
 
40
  vbboxes[:, 3] = kpss[:, 4, 1]
41
  return bboxes, vbboxes
42
 
43
+ def visualize(image: np.ndarray, bboxes: np.ndarray, vbboxes: np.ndarray) -> list[np.ndarray]:
44
+ person_images = []
45
  for i in range(bboxes.shape[0]):
46
  bbox = bboxes[i]
 
47
  x1, y1, x2, y2 = bbox
48
+ person_img = image[y1:y2, x1:x2]
49
+
50
+ # Append the cropped person image
51
+ person_images.append(person_img)
52
+
53
+ return person_images
 
 
 
 
 
54
 
55
  detector = load_model()
56
  detector.prepare(-1, nms_thresh=0.5, input_size=(640, 640))
57
 
58
+ def detect(image: np.ndarray) -> list[np.ndarray]:
59
  if image is None:
60
+ return []
61
 
62
  image = image[:, :, ::-1] # RGB -> BGR
63
  bboxes, vbboxes = detect_person(image, detector)
64
+ person_images = visualize(image, bboxes, vbboxes)
65
+ return [img[:, :, ::-1] for img in person_images] # BGR -> RGB
66
 
67
  examples = sorted(pathlib.Path("images").glob("*.jpg"))
68
 
69
  demo = gr.Interface(
70
  fn=detect,
71
  inputs=gr.Image(label="Input", type="numpy"),
72
+ outputs=gr.Gallery(label="Detected Persons"),
73
  examples=examples,
74
  examples_per_page=30,
75
  title=TITLE,