Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,80 +2,19 @@
|
|
2 |
Run a rest API exposing the yolov5s object detection model
|
3 |
"""
|
4 |
|
5 |
-
import
|
6 |
-
import
|
7 |
-
from flask import Flask, request
|
8 |
-
from PIL import Image
|
9 |
-
from waitress import serve
|
10 |
-
import subprocess
|
11 |
-
import argparse
|
12 |
-
import os
|
13 |
|
14 |
-
'''
|
15 |
-
#subprocess.run(["export", "FLASK_APP","=","app.py"])
|
16 |
-
app = Flask(__name__)
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
#model = torch.hub.load('ultralytics/yolov5', 'custom', path='best2.pt', force_reload=True) # force_reload to recache
|
25 |
-
|
26 |
-
if not request.method == "POST":
|
27 |
-
return
|
28 |
-
|
29 |
-
if request.files.get("image"):
|
30 |
-
image_file = request.files["image"]
|
31 |
-
image_bytes = image_file.read()
|
32 |
-
|
33 |
-
img = Image.open(io.BytesIO(image_bytes))
|
34 |
-
|
35 |
-
results = model(img, size=640) # reduce size=320 for faster inference
|
36 |
-
results=results.pandas().xyxy[0].to_json(orient="records")
|
37 |
-
return f"{results}"
|
38 |
-
|
39 |
-
|
40 |
-
if __name__ == "__main__":
|
41 |
-
|
42 |
-
#subprocess.run(["export","FLASK_ENV","=","development"])
|
43 |
-
app.run(host="0.0.0.0", port=7860) # debug=True causes Restarting with stat
|
44 |
-
#serve(app, host="0.0.0.0", port=7860)
|
45 |
-
|
46 |
-
if __name__ == "__main__":
|
47 |
|
48 |
-
|
49 |
-
app.run(host="0.0.0.0", port=7860,debug =True) # debug=True causes Restarting with stat
|
50 |
-
'''
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
app = Flask(__name__)
|
55 |
-
|
56 |
-
|
57 |
-
@app.route('/')
|
58 |
-
def index():
|
59 |
-
|
60 |
-
'''return '<iframe frameBorder="0" height="100%" src="{}/?__dark-theme={}" width="100%"></iframe>'.format(
|
61 |
-
os.getenv('INACCEL_URL'),request.args.get('__dark-theme', 'false'))'''
|
62 |
-
model = torch.hub.load('ultralytics/yolov5', 'custom', path='best2.pt', force_reload=True) # force_reload to recache
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
img = Image.open(io.BytesIO(image_bytes))
|
69 |
-
|
70 |
-
results = model(img, size=640) # reduce size=320 for faster inference
|
71 |
-
results.imgs # array of original images (as np array) passed to model for inference
|
72 |
-
results.render() # updates results.imgs with boxes and labels
|
73 |
-
for img in results.imgs:
|
74 |
-
buffered = BytesIO()
|
75 |
-
img_base64 = Image.fromarray(img)
|
76 |
-
img_base64.save(buffered, format="JPEG")
|
77 |
-
return base64.b64encode(buffered.getvalue()).decode('utf-8') # base64 encoded image with results
|
78 |
-
|
79 |
-
|
80 |
-
if __name__ == '__main__':
|
81 |
-
app.run(host='0.0.0.0', port=7860)
|
|
|
2 |
Run a rest API exposing the yolov5s object detection model
|
3 |
"""
|
4 |
|
5 |
+
import gradio as gr
|
6 |
+
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
|
|
|
|
|
|
8 |
|
9 |
+
def detect(inp):
|
10 |
+
filename=inp
|
11 |
+
bs64Data=gr.processing_utils.encode_file_to_base64(filename)
|
12 |
+
r = requests.post(url='https://hf.space/gradioiframe/Sa-m/Political-Party-Symbol-Detector-V1/+/api/predict/',
|
13 |
+
json={"data": [bs64Data]})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
return r.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
inp = gr.inputs.Image(type='file', label="Input Image")
|
18 |
+
output=gr.outputs.JSON(label='Response')
|
19 |
+
io=gr.Interface(fn=analysis, inputs=inp, outputs=output, title='Party Symbol Detector API',)
|
20 |
+
io.launch(debug=True,share=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|