eldavid commited on
Commit
1d9b856
1 Parent(s): e65f4ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -38
app.py CHANGED
@@ -1,39 +1,35 @@
1
  import gradio as gr
2
- import subprocess
3
- import os
4
-
5
- def crowd_counting(image):
6
- # Save the uploaded image
7
- image_path = "test/uploaded.jpg"
8
- image.save(image_path)
9
-
10
- # Run the crowd counting model using subprocess
11
-
12
-
13
- command = "python3 detect.py --weights weights/crowdhuman_yolov5m.pt --source {} --head --project runs/output --exist-ok".format(image_path)
14
- subprocess.run(command, shell=True)
15
-
16
- # Read the total_boxes from the file
17
- total_boxes_path = "runs/output/output.txt"
18
- with open(total_boxes_path, "r") as f:
19
- total_boxes = f.read()
20
-
21
- # Get the output image
22
- output_image = "runs/output/output.jpg"
23
-
24
- # Return the output image and total_boxes
25
- return output_image, total_boxes
26
-
27
- # Define the input and output interfaces
28
- inputs = gr.inputs.Image(type="pil", label="Input Image")
29
- outputs = [gr.outputs.Image(type="pil", label="Output Image"), gr.outputs.Textbox(label="Total (Head) Count")]
30
-
31
- # Define the title and description
32
- title = "Crowd Counting"
33
- description = "<div style='text-align: center;'>This is a crowd counting application that uses a deep learning model to count the number of heads in an image.<br>Made by HTX (Q3) </div>"
34
-
35
- # Create the Gradio interface without the flag button
36
- gradio_interface = gr.Interface(fn=crowd_counting, inputs=inputs, outputs=outputs, title=title, description=description, allow_flagging="never")
37
-
38
- # Run the Gradio interface
39
- gradio_interface.launch()
 
1
  import gradio as gr
2
+ from transformers import DetrImageProcessor, DetrForObjectDetection
3
+ import torch
4
+ from PIL import Image
5
+
6
+ # Carregar o processador e o modelo
7
+ processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
8
+ model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
9
+
10
+ def detect_people(image):
11
+ # Pré-processar a imagem
12
+ inputs = processor(images=image, return_tensors="pt")
13
+
14
+ # Realizar a inferência
15
+ outputs = model(**inputs)
16
+
17
+ # Processar os resultados
18
+ target_sizes = torch.tensor([image.size[::-1]])
19
+ results = processor.post_process_object_detection(outputs, target_sizes=target_sizes)[0]
20
+
21
+ # Contar o número de pessoas detectadas
22
+ num_pessoas = sum([1 for score, label in zip(results["scores"], results["labels"]) if score > 0.9 and model.config.id2label[label.item()] == "person"])
23
+ return num_pessoas
24
+
25
+ # Criar a interface do Gradio
26
+ iface = gr.Interface(
27
+ fn=detect_people,
28
+ inputs=gr.Image(type="pil"),
29
+ outputs=gr.Textbox(),
30
+ title="Contador de Pessoas em Imagens",
31
+ description="Carregue uma imagem para contar o número de pessoas detectadas nela."
32
+ )
33
+
34
+ # Executar a aplicação
35
+ iface.launch()