JohnJoelMota commited on
Commit
d701830
·
verified ·
1 Parent(s): 6dd3b43

create app.py

Browse files
Files changed (1) hide show
  1. app.py +143 -0
app.py ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision
3
+ from torchvision.models.detection import FasterRCNN_ResNet50_FPN_Weights
4
+ from PIL import Image
5
+ import numpy as np
6
+ import matplotlib.pyplot as plt
7
+ import gradio as gr
8
+ import os
9
+ import sys
10
+
11
+ # Load the pre-trained model once
12
+ model = torchvision.models.detection.fasterrcnn_resnet50_fpn(weights=FasterRCNN_ResNet50_FPN_Weights.DEFAULT)
13
+ model.eval()
14
+
15
+ # COCO class names
16
+ COCO_INSTANCE_CATEGORY_NAMES = [
17
+ '__background__', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
18
+ 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'N/A', 'stop sign',
19
+ 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
20
+ 'elephant', 'bear', 'zebra', 'giraffe', 'N/A', 'backpack', 'umbrella', 'N/A', 'N/A',
21
+ 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
22
+ 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket',
23
+ 'bottle', 'N/A', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl',
24
+ 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
25
+ 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'N/A', 'dining table',
26
+ 'N/A', 'N/A', 'toilet', 'N/A', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone',
27
+ 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'N/A', 'book',
28
+ 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush'
29
+ ]
30
+
31
+ # Gradio-compatible detection function
32
+ def detect_objects(image, threshold=0.5):
33
+ if image is None:
34
+ print("Image is None, returning empty output", file=sys.stderr)
35
+ # Create a blank image as output
36
+ blank_img = Image.new('RGB', (400, 400), color='white')
37
+ plt.figure(figsize=(10, 10))
38
+ plt.imshow(blank_img)
39
+ plt.text(0.5, 0.5, "No image provided",
40
+ horizontalalignment='center', verticalalignment='center',
41
+ transform=plt.gca().transAxes, fontsize=20)
42
+ plt.axis('off')
43
+ output_path = "blank_output.png"
44
+ plt.savefig(output_path)
45
+ plt.close()
46
+ return output_path
47
+
48
+ try:
49
+ print(f"Processing image of type {type(image)} and threshold {threshold}", file=sys.stderr)
50
+ # Make sure threshold is a valid number
51
+ if threshold is None:
52
+ threshold = 0.5
53
+ print("Threshold was None, using default 0.5", file=sys.stderr)
54
+
55
+ # Convert threshold to float if it's not already
56
+ threshold = float(threshold)
57
+
58
+ transform = FasterRCNN_ResNet50_FPN_Weights.DEFAULT.transforms()
59
+ image_tensor = transform(image).unsqueeze(0)
60
+
61
+ with torch.no_grad():
62
+ prediction = model(image_tensor)[0]
63
+
64
+ boxes = prediction['boxes'].cpu().numpy()
65
+ labels = prediction['labels'].cpu().numpy()
66
+ scores = prediction['scores'].cpu().numpy()
67
+
68
+ image_np = np.array(image)
69
+ plt.figure(figsize=(10, 10))
70
+ plt.imshow(image_np)
71
+ ax = plt.gca()
72
+
73
+ for box, label, score in zip(boxes, labels, scores):
74
+ # Explicit debug prints to trace the comparison issue
75
+ print(f"Score: {score}, Threshold: {threshold}, Type: {type(score)}/{type(threshold)}", file=sys.stderr)
76
+
77
+ if score >= threshold:
78
+ x1, y1, x2, y2 = box
79
+ ax.add_patch(plt.Rectangle((x1, y1), x2 - x1, y2 - y1,
80
+ fill=False, color='red', linewidth=2))
81
+ class_name = COCO_INSTANCE_CATEGORY_NAMES[label]
82
+ ax.text(x1, y1, f'{class_name}: {score:.2f}', bbox=dict(facecolor='yellow', alpha=0.5),
83
+ fontsize=12, color='black')
84
+
85
+ plt.axis('off')
86
+ plt.tight_layout()
87
+
88
+ # Save the figure to return
89
+ output_path = "output.png"
90
+ plt.savefig(output_path)
91
+ plt.close()
92
+ return output_path
93
+ except Exception as e:
94
+ print(f"Error in detect_objects: {e}", file=sys.stderr)
95
+ import traceback
96
+ traceback.print_exc(file=sys.stderr)
97
+
98
+ # Create an error image
99
+ error_img = Image.new('RGB', (400, 400), color='white')
100
+ plt.figure(figsize=(10, 10))
101
+ plt.imshow(error_img)
102
+ plt.text(0.5, 0.5, f"Error: {str(e)}",
103
+ horizontalalignment='center', verticalalignment='center',
104
+ transform=plt.gca().transAxes, fontsize=12, wrap=True)
105
+ plt.axis('off')
106
+ error_path = "error_output.png"
107
+ plt.savefig(error_path)
108
+ plt.close()
109
+ return error_path
110
+
111
+ # Create direct file paths for examples
112
+ # These exact filenames match what's visible in your repository
113
+ examples = [
114
+ os.path.join("/home/user/app", "TEST_IMG_1.jpg"),
115
+ os.path.join("/home/user/app", "TEST_IMG_2.JPG"),
116
+ os.path.join("/home/user/app", "TEST_IMG_3.jpg"),
117
+ os.path.join("/home/user/app", "TEST_IMG_4.jpg")
118
+ ]
119
+
120
+ # Create Gradio interface
121
+ # Important: For Gradio examples, we need to create a list of lists
122
+ example_list = [[path] for path in examples if os.path.exists(path)]
123
+
124
+ print(f"Found {len(example_list)} valid examples: {example_list}", file=sys.stderr)
125
+
126
+ # Create Gradio interface with a simplified approach
127
+ interface = gr.Interface(
128
+ fn=detect_objects,
129
+ inputs=[
130
+ gr.Image(type="pil", label="Input Image"),
131
+ gr.Slider(minimum=0.0, maximum=1.0, value=0.5, step=0.05, label="Confidence Threshold")
132
+ ],
133
+ outputs=gr.Image(type="filepath", label="Detected Objects"),
134
+ title="Faster R-CNN Object Detection",
135
+ description="Upload an image to detect objects using a pretrained Faster R-CNN model.",
136
+ examples=example_list,
137
+ cache_examples=False # Disable caching to avoid potential issues
138
+ )
139
+
140
+ # Launch with specific configuration for Hugging Face
141
+ if __name__ == "__main__":
142
+ # Launch with debug mode enabled
143
+ interface.launch(debug=True)