ConservationDrones commited on
Commit
41cbbf0
·
verified ·
1 Parent(s): 55e3afe

Upload HuggingFaceDemo.py

Browse files
Files changed (1) hide show
  1. HuggingFaceDemo.py +108 -0
HuggingFaceDemo.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+
4
+ import torch
5
+ from PIL import Image, ImageDraw, ImageFont
6
+ from transformers import AutoImageProcessor, AutoModelForObjectDetection
7
+
8
+
9
+ # Hugging Face repository
10
+ repo_id = "ConservationDrones/DroneMegaDetector"
11
+
12
+ # Load the demo image, or an image supplied on the command line.
13
+ image_path = sys.argv[1] if len(sys.argv) > 1 else "example.png"
14
+ image = Image.open(image_path).convert("RGB")
15
+
16
+
17
+ # Load the processor and model from Hugging Face.
18
+ processor = AutoImageProcessor.from_pretrained(
19
+ repo_id,
20
+ )
21
+
22
+ model = AutoModelForObjectDetection.from_pretrained(
23
+ repo_id,
24
+ ).eval()
25
+
26
+
27
+ # Use 4 CPU threads, as in the original local demo.
28
+ torch.set_num_threads(4)
29
+
30
+
31
+ # Preprocess the image and run inference.
32
+ encoded = processor(
33
+ images=image,
34
+ return_tensors="pt",
35
+ )
36
+
37
+ with torch.inference_mode():
38
+ outputs = model(**encoded)
39
+
40
+
41
+ # Keep scores above 0.3 and map boxes back to the original image dimensions.
42
+ results = processor.post_process_object_detection(
43
+ outputs,
44
+ threshold=0.3,
45
+ target_sizes=[(image.height, image.width)],
46
+ )[0]
47
+
48
+
49
+ # Print the class, confidence, and [xmin, ymin, xmax, ymax] in source pixels.
50
+ print(
51
+ f"{image_path}: {len(results['scores'])} detections (score >= 0.3)"
52
+ )
53
+
54
+ for score, label, box in zip(
55
+ results["scores"],
56
+ results["labels"],
57
+ results["boxes"],
58
+ ):
59
+ print(
60
+ model.config.id2label[label.item()],
61
+ f"{score.item():.3f}",
62
+ [round(v, 1) for v in box.tolist()],
63
+ )
64
+
65
+
66
+ # Draw the detections.
67
+ draw = ImageDraw.Draw(image)
68
+ font = ImageFont.load_default(size=18)
69
+
70
+ for score, label, box in zip(
71
+ results["scores"],
72
+ results["labels"],
73
+ results["boxes"],
74
+ ):
75
+ bounds = box.tolist()
76
+
77
+ text = (
78
+ f"{model.config.id2label[label.item()]} "
79
+ f"{score.item():.2f}"
80
+ )
81
+
82
+ draw.rectangle(
83
+ bounds,
84
+ outline="red",
85
+ width=3,
86
+ )
87
+
88
+ text_x = min(
89
+ max(2, bounds[0]),
90
+ image.width - draw.textlength(text, font=font) - 2,
91
+ )
92
+
93
+ draw.text(
94
+ (text_x, max(0, bounds[1] - 20)),
95
+ text,
96
+ font=font,
97
+ fill="white",
98
+ stroke_width=2,
99
+ stroke_fill="black",
100
+ )
101
+
102
+
103
+ # Save the annotated image.
104
+ output_path = "example_annotated.jpg"
105
+ image.save(output_path, quality=95)
106
+
107
+ print(f"Saved {output_path}")
108
+