Skip to content

Commit

Permalink
v. 0.2.1
Browse files Browse the repository at this point in the history
Added 'resize' input parameter
  • Loading branch information
mrekin committed Apr 13, 2021
1 parent db232b9 commit fa7619d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Add request params to URL if needed:
* _resimg_ - to return result image with objects marked as base64 string (False is default, any other value is eq True)
* _autorotation_ - autorotate image using exif data (Orientation) (False is default, any other value is eq True)
* _rotation=<value>_ - rotate to <value> degrees before analisys. Works with/without _autorotation_
* _resize=<value>_ - resize to <value> px (max side of image) before analisys. If value not passed: 1000px is default.

`curl --request POST -F "file=@IMG.JPG" localhost:5000/api/v1.0/imgrecognize/?exif=False&autorotation&rotation=90`

Expand Down Expand Up @@ -78,4 +79,4 @@ sys 0m0.023s
* _DONE:_ add some variables for request (like `?noexif` or `?noobjects`)
* add basic-auth for service
* _DONE_: add posibility use not only segmentation models
* add possibility to resize image (__panoptic segmentation causes docker crash if input image too big__, may be to low RAM, so resize image is good point to solve this and increase recognition speed
* _DONE_: add possibility to resize image (__panoptic segmentation causes docker crash if input image too big__, may be to low RAM, so resize image is good point to solve this and increase recognition speed
5 changes: 3 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ services:
volumes:
- d2test:/home/d2test
environment:
- SEGMENTATION_MODEL=COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml
# - SEGMENTATION_MODEL=COCO-PanopticSegmentation/panoptic_fpn_R_50_1x.yaml
# - SEGMENTATION_MODEL=COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml
- SEGMENTATION_MODEL=COCO-PanopticSegmentation/panoptic_fpn_R_50_3x.yaml
# - SEGMENTATION_MODEL=Cityscapes/mask_rcnn_R_50_FPN.yaml
- FLASK_DEBUG=True
- FLASK_HOST=0.0.0.0
- FLASK_PORT=5000
Expand Down
8 changes: 4 additions & 4 deletions html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
<html>
<head>
<meta charset="utf-8">
<title>Выберите изображение</title>
<title>Get image</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>


</head>
<body>
<form id = "form" enctype="multipart/form-data" method="post">
<label for="files" class="btn">Выберите изображение</label>
<p><input type="file" name="f" id="file" title="Выберите изображение">
<input type="submit" value="Отправить">
<label for="files" class="btn">Get image</label>
<p><input type="file" name="f" id="file" title="Get image">
<input type="submit" value="Submit">
<input type="hidden" name="rotation" id="rotation" value="0"/>
<img id = "loading" height = "25px" /></p>
</form>
Expand Down
29 changes: 25 additions & 4 deletions service.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,16 @@ def rotate(img, args, exif):
return img


def resize(img, size):
maxwh = max(img.width, img.height)
if maxwh <= size:
img = img
else:
max_ratio = size / maxwh
size = (int(img.width*max_ratio), int(img.height*max_ratio))
img = img.resize(size)
return img



#############################################################################
Expand All @@ -225,6 +235,7 @@ def rotate(img, args, exif):
app.config['JSON_SORT_KEYS'] = False

def prepareArgs(args):
reqArgs = reqArgsDef
arg = 'autorotation'
if arg in args and args[arg] is not False:
reqArgs[arg] = True
Expand All @@ -241,15 +252,22 @@ def prepareArgs(args):
if arg in args and args[arg] is not False:
reqArgs[arg] = True

arg = 'resize'
if arg in args and args[arg] is not '0' and args[arg] is not '':
reqArgs[arg] = args[arg]
elif arg in args and args[arg] is '':
reqArgsDef[arg] = 1000
return reqArgs

reqArgsDef = {
'autorotation': False,
'rotation': 0,
'exif' : False,
'resimg': False
'resimg': False,
'resize' : 0
}

reqArgs = reqArgsDef


######################################################################
########### URLS
Expand All @@ -266,11 +284,11 @@ def index():
def upload_file():
resp ={}
if request.method == 'POST':
reqArgs = reqArgsDef
reqArgs = prepareArgs(request.args)
print (request.files.keys)
for fn in request.files:
file = request.files[fn]
prepareArgs(request.args)


if file:
filename = secure_filename(file.filename)
Expand All @@ -284,6 +302,9 @@ def upload_file():

if reqArgs['rotation'] is not 0 or reqArgs['autorotation'] is True:
img = rotate(img, reqArgs, exif)

if reqArgs['resize'] is not 0:
img = resize(img, reqArgs['resize'])

resp[filename]['objects'] , resp[filename]['segments'], out = analizeImg(img)
resp[filename]['objectsShortList'] = getLabesShortList(resp[filename]['objects'])
Expand Down
2 changes: 1 addition & 1 deletion static/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ $( '#form' )

$.ajax( {
// url: '/api/v1.0/imgrecognize/?rotation='+rotation,
url: '/api/v1.0/imgrecognize/?exif&resimg&autorotation&rotation='+rotation,
url: '/api/v1.0/imgrecognize/?exif&resimg&autorotation&resize&rotation='+rotation,
type: 'POST',
enctype: 'multipart/form-data',
data: data,
Expand Down

0 comments on commit fa7619d

Please sign in to comment.