crisrm128 commited on
Commit
95f629f
·
1 Parent(s): c54658c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -38
app.py CHANGED
@@ -1,44 +1,56 @@
1
- from fastapi import FastAPI
2
- from transformers import pipeline, YolosForObjectDetection, YolosImageProcessor
3
- from PIL import Image
4
  import torch
5
  import requests
 
 
6
 
7
- # Create a new FastAPI app instance
8
  app = FastAPI()
9
 
10
- # Initialize the Yolos model and image processor
11
- yolos_model = YolosForObjectDetection.from_pretrained('hustvl/yolos-tiny')
12
- yolos_image_processor = YolosImageProcessor.from_pretrained("hustvl/yolos-tiny")
13
-
14
- # Route for object detection using Yolos model
15
- @app.get("/detect-objects")
16
  def detect_objects(url: str):
17
- # Download the image from the specified URL
18
- image = Image.open(requests.get(url, stream=True).raw)
19
-
20
- # Preprocess the image using the Yolos image processor
21
- inputs = yolos_image_processor(images=image, return_tensors="pt")
22
-
23
- # Run the Yolos model on the preprocessed image
24
- outputs = yolos_model(**inputs)
25
-
26
- # model predicts bounding boxes and corresponding COCO classes
27
- logits = outputs.logits
28
- pred_boxes = outputs.pred_boxes
29
-
30
- # Post-process the object detection results
31
- target_sizes = torch.tensor([image.size[::-1]])
32
- results = yolos_image_processor.post_process_object_detection(outputs, threshold=0.9, target_sizes=target_sizes)[0]
33
-
34
- # Format and return the results
35
- detected_objects = []
36
- for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
37
- box = [round(i, 2) for i in box.tolist()]
38
- detected_objects.append({
39
- "label": yolos_model.config.id2label[label.item()],
40
- "confidence": round(score.item(), 3),
41
- "location": box
42
- })
43
-
44
- return {"detected_objects": detected_objects}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ffrom fastapi import FastAPI, HTTPException, Response
2
+ from fastapi.responses import HTMLResponse
3
+ from PIL import Image, ImageDraw
4
  import torch
5
  import requests
6
+ import io
7
+ import base64
8
 
 
9
  app = FastAPI()
10
 
11
+ @app.get("/detect-objects", response_class=HTMLResponse)
 
 
 
 
 
12
  def detect_objects(url: str):
13
+ try:
14
+ # Download the image from the specified URL
15
+ image = Image.open(requests.get(url, stream=True).raw)
16
+
17
+ # Preprocess the image using the Yolos image processor
18
+ inputs = yolos_image_processor(images=image, return_tensors="pt")
19
+
20
+ # Run the Yolos model on the preprocessed image
21
+ outputs = yolos_model(**inputs)
22
+
23
+ # model predicts bounding boxes and corresponding COCO classes
24
+ logits = outputs.logits
25
+ pred_boxes = outputs.pred_boxes
26
+
27
+ # Post-process the object detection results
28
+ target_sizes = torch.tensor([image.size[::-1]])
29
+ results = yolos_image_processor.post_process_object_detection(outputs, threshold=0.9, target_sizes=target_sizes)[0]
30
+
31
+ # Draw bounding boxes on the image
32
+ for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
33
+ image_draw = ImageDraw.Draw(image)
34
+ image_draw.rectangle(box.tolist(), outline="red", width=2)
35
+ image_draw.text((box[0], box[1]), f"{yolos_model.config.id2label[label.item()]}: {round(score.item(), 3)}", fill="red")
36
+
37
+ # Save the modified image to a byte stream
38
+ image_byte_array = io.BytesIO()
39
+ image.save(image_byte_array, format="PNG")
40
+
41
+ # Encode the byte stream as a base64 string
42
+ image_base64 = base64.b64encode(image_byte_array.getvalue()).decode("utf-8")
43
+
44
+ # Create a custom HTML response
45
+ html_content = f"""
46
+ <html>
47
+ <body>
48
+ <img src="data:image/png;base64,{image_base64}" alt="Detected Objects">
49
+ </body>
50
+ </html>
51
+ """
52
+ return HTMLResponse(content=html_content)
53
+
54
+ except Exception as e:
55
+ raise HTTPException(status_code=500, detail=f"Error processing image: {str(e)}")
56
+