pratikshahp commited on
Commit
43f3f52
·
verified ·
1 Parent(s): 2356623

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -18
app.py CHANGED
@@ -2,42 +2,43 @@ from transformers import DetrImageProcessor, DetrForObjectDetection
2
  from PIL import Image, ImageDraw
3
  import torch
4
  import gradio as gr
5
- import requests
6
- from io import BytesIO
7
 
8
- # Load pre-trained DETR model
9
  processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
10
  model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
11
 
12
- # COCO class index for "person" = 1 (used as proxy for face detection)
13
- FACE_CLASS_INDEX = 1
14
 
15
  def detect_faces(img: Image.Image):
16
- # Prepare input for the model
 
 
 
 
17
  inputs = processor(images=img, return_tensors="pt")
18
  outputs = model(**inputs)
19
 
20
- # Get outputs
21
  target_sizes = torch.tensor([img.size[::-1]])
22
- results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
23
 
24
- # Draw bounding boxes
25
- draw = ImageDraw.Draw(img)
26
  for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
27
- if label.item() == FACE_CLASS_INDEX: # 'person'
 
28
  box = [round(i, 2) for i in box.tolist()]
29
- draw.rectangle(box, outline="green", width=3)
30
- draw.text((box[0], box[1]), f"{score:.2f}", fill="green")
31
 
32
- return img
33
 
34
- # Gradio interface
35
  iface = gr.Interface(
36
  fn=detect_faces,
37
  inputs=gr.Image(type="pil"),
38
- outputs="image",
39
- title="Face Detection App (Hugging Face + Gradio)",
40
- description="Upload an image and detect faces using facebook/detr-resnet-50 model."
41
  )
42
 
43
  iface.launch()
 
2
  from PIL import Image, ImageDraw
3
  import torch
4
  import gradio as gr
 
 
5
 
6
+ # Load model and processor
7
  processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
8
  model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
9
 
10
+ FACE_CLASS_INDEX = 1 # COCO class ID for 'person'
 
11
 
12
  def detect_faces(img: Image.Image):
13
+ # Make a copy to draw on
14
+ img_draw = img.copy()
15
+ draw = ImageDraw.Draw(img_draw)
16
+
17
+ # Preprocess and predict
18
  inputs = processor(images=img, return_tensors="pt")
19
  outputs = model(**inputs)
20
 
21
+ # Get results
22
  target_sizes = torch.tensor([img.size[::-1]])
23
+ results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.8)[0]
24
 
25
+ count = 0
 
26
  for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
27
+ if label.item() == FACE_CLASS_INDEX:
28
+ count += 1
29
  box = [round(i, 2) for i in box.tolist()]
30
+ draw.rectangle(box, outline="lime", width=3)
31
+ draw.text((box[0], box[1] - 10), f"{score:.2f}", fill="lime")
32
 
33
+ return img_draw, f"Total Persons Detected: {count}"
34
 
35
+ # Gradio Interface
36
  iface = gr.Interface(
37
  fn=detect_faces,
38
  inputs=gr.Image(type="pil"),
39
+ outputs=[gr.Image(type="pil"), gr.Text()],
40
+ title="Person Detection with DETR",
41
+ description="Uses DETR model to detect people (class 1 - COCO dataset). Note: not specialized for face detection."
42
  )
43
 
44
  iface.launch()