Update app.py
Browse files
app.py
CHANGED
|
@@ -86,6 +86,10 @@ def show_mask(mask, ax, random_color=False):
|
|
| 86 |
def process_image_detection(image, target_label, surprise_rating):
|
| 87 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
owlv2_processor = Owlv2Processor.from_pretrained("google/owlv2-large-patch14")
|
| 90 |
owlv2_model = Owlv2ForObjectDetection.from_pretrained("google/owlv2-large-patch14").to(device)
|
| 91 |
|
|
@@ -101,9 +105,16 @@ def process_image_detection(image, target_label, surprise_rating):
|
|
| 101 |
target_sizes = torch.tensor([image.size[::-1]]).to(device)
|
| 102 |
results = owlv2_processor.post_process_object_detection(outputs, target_sizes=target_sizes)[0]
|
| 103 |
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
plt.imshow(image)
|
| 106 |
-
ax = plt.gca()
|
| 107 |
|
| 108 |
scores = results["scores"]
|
| 109 |
if len(scores) > 0:
|
|
@@ -157,12 +168,26 @@ def process_image_detection(image, target_label, surprise_rating):
|
|
| 157 |
|
| 158 |
plt.axis('off')
|
| 159 |
|
|
|
|
| 160 |
buf = io.BytesIO()
|
| 161 |
-
plt.savefig(buf,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
buf.seek(0)
|
| 163 |
plt.close()
|
| 164 |
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
|
| 168 |
def process_and_analyze(image):
|
|
@@ -181,15 +206,14 @@ def process_and_analyze(image):
|
|
| 181 |
gpt_response = analyze_image(image)
|
| 182 |
response_data = json.loads(gpt_response)
|
| 183 |
|
| 184 |
-
analysis_text = f"Label: {response_data['label']}\nElement: {response_data['element']}\nRating: {response_data['rating']}/5"
|
| 185 |
-
|
| 186 |
if response_data["label"].lower() == "surprising" and response_data["element"].lower() != "na":
|
| 187 |
# Process image with detection models
|
| 188 |
result_buf = process_image_detection(image, response_data["element"], response_data["rating"])
|
| 189 |
result_image = Image.open(result_buf)
|
|
|
|
| 190 |
return result_image, analysis_text
|
| 191 |
else:
|
| 192 |
-
return image,
|
| 193 |
|
| 194 |
except Exception as e:
|
| 195 |
return None, f"Error processing image: {str(e)}"
|
|
|
|
| 86 |
def process_image_detection(image, target_label, surprise_rating):
|
| 87 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 88 |
|
| 89 |
+
# Get original image DPI and size
|
| 90 |
+
original_dpi = image.info.get('dpi', (72, 72)) # Default to 72 DPI if not specified
|
| 91 |
+
original_size = image.size
|
| 92 |
+
|
| 93 |
owlv2_processor = Owlv2Processor.from_pretrained("google/owlv2-large-patch14")
|
| 94 |
owlv2_model = Owlv2ForObjectDetection.from_pretrained("google/owlv2-large-patch14").to(device)
|
| 95 |
|
|
|
|
| 105 |
target_sizes = torch.tensor([image.size[::-1]]).to(device)
|
| 106 |
results = owlv2_processor.post_process_object_detection(outputs, target_sizes=target_sizes)[0]
|
| 107 |
|
| 108 |
+
# Create figure with the exact pixel size of the original image
|
| 109 |
+
dpi = 100 # Base DPI for calculation
|
| 110 |
+
figsize = (original_size[0] / dpi, original_size[1] / dpi)
|
| 111 |
+
fig = plt.figure(figsize=figsize, dpi=dpi)
|
| 112 |
+
|
| 113 |
+
# Remove margins and spacing
|
| 114 |
+
ax = plt.Axes(fig, [0., 0., 1., 1.])
|
| 115 |
+
fig.add_axes(ax)
|
| 116 |
+
|
| 117 |
plt.imshow(image)
|
|
|
|
| 118 |
|
| 119 |
scores = results["scores"]
|
| 120 |
if len(scores) > 0:
|
|
|
|
| 168 |
|
| 169 |
plt.axis('off')
|
| 170 |
|
| 171 |
+
# Save with original resolution and DPI
|
| 172 |
buf = io.BytesIO()
|
| 173 |
+
plt.savefig(buf,
|
| 174 |
+
format='png',
|
| 175 |
+
dpi=dpi,
|
| 176 |
+
bbox_inches='tight',
|
| 177 |
+
pad_inches=0)
|
| 178 |
buf.seek(0)
|
| 179 |
plt.close()
|
| 180 |
|
| 181 |
+
# Open the buffer and create a new image with original properties
|
| 182 |
+
output_image = Image.open(buf)
|
| 183 |
+
output_image = output_image.resize(original_size, Image.Resampling.LANCZOS)
|
| 184 |
+
|
| 185 |
+
# Create a new buffer with the properly sized image
|
| 186 |
+
final_buf = io.BytesIO()
|
| 187 |
+
output_image.save(final_buf, format='PNG', dpi=original_dpi)
|
| 188 |
+
final_buf.seek(0)
|
| 189 |
+
|
| 190 |
+
return final_buf
|
| 191 |
|
| 192 |
|
| 193 |
def process_and_analyze(image):
|
|
|
|
| 206 |
gpt_response = analyze_image(image)
|
| 207 |
response_data = json.loads(gpt_response)
|
| 208 |
|
|
|
|
|
|
|
| 209 |
if response_data["label"].lower() == "surprising" and response_data["element"].lower() != "na":
|
| 210 |
# Process image with detection models
|
| 211 |
result_buf = process_image_detection(image, response_data["element"], response_data["rating"])
|
| 212 |
result_image = Image.open(result_buf)
|
| 213 |
+
analysis_text = f"Label: {response_data['label']}\nElement: {response_data['element']}\nRating: {response_data['rating']}/5"
|
| 214 |
return result_image, analysis_text
|
| 215 |
else:
|
| 216 |
+
return image, "Not Surprising"
|
| 217 |
|
| 218 |
except Exception as e:
|
| 219 |
return None, f"Error processing image: {str(e)}"
|