danieaneta commited on
Commit
2010bf2
·
verified ·
1 Parent(s): 945be6a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -70
app.py CHANGED
@@ -1,71 +1,68 @@
1
- import gradio as gr
2
- from PIL import Image
3
- import base64
4
- import io
5
- import numpy as np
6
- from typing import List
7
- from main import segmenter # Import the segmenter instance
8
-
9
- def process_image(image: Image.Image, objects_text: str) -> dict:
10
- """Process image and return results"""
11
- try:
12
- # Parse objects
13
- objects = [obj.strip() for obj in objects_text.split('.') if obj.strip()]
14
-
15
- # Use the segmenter to process the image
16
- results = segmenter.segment_objects(image, objects)
17
-
18
- # Create visualization of results
19
- # For now, just returning the original image
20
- buffered = io.BytesIO()
21
- image.save(buffered, format="PNG")
22
- img_str = base64.b64encode(buffered.getvalue()).decode()
23
-
24
- # Format results for response
25
- return {
26
- "success": True,
27
- "message": f"Processed image with objects: {objects}",
28
- "image": img_str,
29
- "results": [
30
- {
31
- "label": r.label,
32
- "confidence": float(r.confidence),
33
- "bounding_box": r.bounding_box
34
- }
35
- for r in results
36
- ]
37
- }
38
- except Exception as e:
39
- return {
40
- "success": False,
41
- "message": str(e),
42
- "image": None,
43
- "results": []
44
- }
45
-
46
- # Create Gradio interface with API mode enabled
47
- demo = gr.Interface(
48
- fn=process_image,
49
- inputs=[
50
- gr.Image(type="pil", label="Input Image"),
51
- gr.Textbox(label="Objects (separate with dots)", placeholder="cat. dog. chair")
52
- ],
53
- outputs=gr.JSON(label="API Response"),
54
- title="Zero Shot Segmentation",
55
- description="Upload an image and specify objects to detect.",
56
- allow_flagging="never",
57
- examples=[
58
- ["path/to/example.jpg", "cat. dog"]
59
- ]
60
- )
61
-
62
- # Enable API access
63
- demo.queue()
64
-
65
- if __name__ == "__main__":
66
- demo.launch(
67
- share=True,
68
- server_name="0.0.0.0",
69
- server_port=7860,
70
- show_api=True
71
  )
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+ import base64
4
+ import io
5
+ import numpy as np
6
+ from typing import List
7
+ from main import segmenter # Import the segmenter instance
8
+
9
+ def process_image(image: Image.Image, objects_text: str) -> dict:
10
+ """Process image and return results"""
11
+ try:
12
+ # Parse objects
13
+ objects = [obj.strip() for obj in objects_text.split('.') if obj.strip()]
14
+
15
+ # Use the segmenter to process the image
16
+ results = segmenter.segment_objects(image, objects)
17
+
18
+ # Create visualization of results
19
+ # For now, just returning the original image
20
+ buffered = io.BytesIO()
21
+ image.save(buffered, format="PNG")
22
+ img_str = base64.b64encode(buffered.getvalue()).decode()
23
+
24
+ # Format results for response
25
+ return {
26
+ "success": True,
27
+ "message": f"Processed image with objects: {objects}",
28
+ "image": img_str,
29
+ "results": [
30
+ {
31
+ "label": r.label,
32
+ "confidence": float(r.confidence),
33
+ "bounding_box": r.bounding_box
34
+ }
35
+ for r in results
36
+ ]
37
+ }
38
+ except Exception as e:
39
+ return {
40
+ "success": False,
41
+ "message": str(e),
42
+ "image": None,
43
+ "results": []
44
+ }
45
+
46
+ # Create Gradio interface with API mode enabled
47
+ demo = gr.Interface(
48
+ fn=process_image,
49
+ inputs=[
50
+ gr.Image(type="pil", label="Input Image"),
51
+ gr.Textbox(label="Objects (separate with dots)", placeholder="cat. dog. chair")
52
+ ],
53
+ outputs=gr.JSON(label="API Response"),
54
+ title="Zero Shot Segmentation",
55
+ description="Upload an image and specify objects to detect.",
56
+ allow_flagging="never"
57
+ )
58
+
59
+ # Enable API access
60
+ demo.queue()
61
+
62
+ if __name__ == "__main__":
63
+ demo.launch(
64
+ share=True,
65
+ server_name="0.0.0.0",
66
+ server_port=7860,
67
+ show_api=True
 
 
 
68
  )