-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added mimetype to file data sent to model #39
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
import collections | ||
import json | ||
import logging | ||
import mimetypes | ||
import os | ||
import requests | ||
import signal | ||
|
@@ -120,7 +121,8 @@ def valid_file_ext(filename): | |
|
||
# Runs ML on given image | ||
def run_ml(img_path): | ||
img_file = {'image': open(img_path, 'rb')} | ||
mime_type = mimetypes.guess_type(img_path)[0] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reading the docs, it's fine to assume https://docs.python.org/2/library/mimetypes.html#mimetypes.guess_type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah I looked into that to make sure and the return is always a "pair" |
||
img_file = {'image': (img_path, open(img_path, 'rb'), mime_type)} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I'm not mistaken the file handle returned by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch, odd that no one ever caught that before, not that it matter much given this file is opened in a thread. I'll close it. |
||
r = requests.post(url=ml_endpoint, files=img_file) | ||
cap_json = r.json() | ||
caption = cap_json['predictions'] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you tested what happens when no mime-type can be guessed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested with the three valid extensions, any other extension would be rejected before getting to this line. Given this library guesses mime type by the file extension (IIUC), that should be enough. I originally used magic to do this (more robust, not a guess), but it requires installs outside pip, which I didn't want to add.