Skip to content
This repository has been archived by the owner on Jul 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #35 from robmarkcole/ISSUE33
Browse files Browse the repository at this point in the history
solve issue
  • Loading branch information
robmarkcole authored Dec 6, 2022
2 parents ac18396 + e3d437a commit e3e3eaa
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ Simple app consisting of a form where you can upload an image, and see the infer
then visit http://localhost:5000/ in your browser:

<p align="center">
<img src="https://github.com/robmarkcole/yolov5-flask/blob/master/docs/app_form.jpg" width="450">
<img src="docs/app_form.jpg" width="450">
</p>

<p align="center">
<img src="https://github.com/robmarkcole/yolov5-flask/blob/master/docs/app_result.jpg" width="450">
<img src="docs/app_result.jpg" width="450">
</p>

## Rest API
Simple rest API exposing the model for consumption by another service. Run:

`$ python3 restapi.py --port 5000`
`$ python3 restapi.py --port 5000 --model yolov5s`

Then use [curl](https://curl.se/) to perform a request:

`$ curl -X POST -F image=@tests/zidane.jpg 'http://localhost:5000/v1/object-detection/yolov5s'`
`$ curl -X POST -F image=@tests/zidane.jpg 'http://localhost:5000/v1/object-detection/yolov5'`

The model inference results are returned:

Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
flask
requests

# YOLOv5 requirements
matplotlib>=3.2.2
Expand Down
12 changes: 5 additions & 7 deletions restapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

app = Flask(__name__)

DETECTION_URL = "/v1/object-detection/yolov5s"
DETECTION_URL = "/v1/object-detection/yolov5"


@app.route(DETECTION_URL, methods=["POST"])
Expand All @@ -22,16 +22,14 @@ def predict():
image_file = request.files["image"]
image_bytes = image_file.read()
img = Image.open(io.BytesIO(image_bytes))
results = model(img, size=640)
data = results.pandas().xyxy[0].to_json(orient="records")
return data

results = model(img, size=640) # reduce size=320 for faster inference
return results.pandas().xyxy[0].to_json(orient="records")

if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Flask api exposing yolov5 model")
parser.add_argument("--port", default=5000, type=int, help="port number")
parser.add_argument('--model', default='yolov5s', help='model to run, i.e. --model yolov5s')
args = parser.parse_args()

model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True) # force_reload = recache latest code
model.eval()
model = torch.hub.load('ultralytics/yolov5', args.model)
app.run(host="0.0.0.0", port=args.port) # debug=True causes Restarting with stat

0 comments on commit e3e3eaa

Please sign in to comment.