saketh-005 commited on
Commit
b6437c4
·
verified ·
1 Parent(s): 947d8e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -1
app.py CHANGED
@@ -139,4 +139,41 @@ async def detect(request: Request):
139
  )
140
 
141
  result = process_image_np(image_np)
142
- return result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
  )
140
 
141
  result = process_image_np(image_np)
142
+ return result
143
+
144
+
145
+ from fastapi import Response
146
+
147
+ # ----------------------------
148
+ # Convert Image To JPEG (For Browser Display)
149
+ # ----------------------------
150
+
151
+ @app.post("/convert")
152
+ async def convert(request: Request):
153
+ body = await request.body()
154
+
155
+ if not body:
156
+ return JSONResponse(
157
+ {"error": "Empty request body"},
158
+ status_code=400
159
+ )
160
+
161
+ image_np = decode_image(body)
162
+
163
+ if image_np is None:
164
+ return JSONResponse(
165
+ {"error": "Unsupported or invalid image format"},
166
+ status_code=400
167
+ )
168
+
169
+ # Convert BGR -> RGB
170
+ image_rgb = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
171
+ pil_image = Image.fromarray(image_rgb)
172
+
173
+ buffer = io.BytesIO()
174
+ pil_image.save(buffer, format="JPEG", quality=90)
175
+
176
+ return Response(
177
+ content=buffer.getvalue(),
178
+ media_type="image/jpeg"
179
+ )