def call_distort_image(image_id=None): ''' Distorts an image according to the distortion pairs in the specified distortion set ''' try: new_image_id = uuid.uuid4() picture_dict = get_document_with_exception(image_id, document_type='picture') args_dict = gather_and_enforce_request_args([{ 'name': 'distortion_set_id', 'required': True }]) distortion_set_id = args_dict['distortion_set_id'] distortion_set_dict = get_document_with_exception( distortion_set_id, document_type='distortion_set') # TODO call this async via distort_image_shepards_task.delay as soon as it's working ans.distort_image_shepards(image_id_in=image_id, image_id_out=new_image_id, distortion_set_id=distortion_set_id) resp_json = {'distorted_image_id': str(new_image_id)} return Response(json.dumps(resp_json), status=202, mimetype='application/json') except Exception as e: return Response(json.dumps(e.message), status=e.status_code, mimetype='application/json')
def test_get_document_with_exception_throws_exception_when_document_of_specified_type_doesnt_exist(self): doc_id = uuid.uuid4() dict_in = {'_id': doc_id, 'type': 'twix'} tu.save_document(dict_in) with pytest.raises(NotFoundError) as exception_info: tu.get_document_with_exception(doc_id, document_type='skittles') error_string = 'No document of type skittles found for id {0}'.format(str(doc_id)) assert error_string in str(exception_info.value)
def test_get_document_with_exception_throws_exception_when_no_document_of_any_type_exists_for_requested_id(self): doc_id = uuid.uuid4() dict_in = {'_id': doc_id, 'type': 'twix'} tu.save_document(dict_in) second_doc_id = uuid.uuid4() with pytest.raises(NotFoundError) as exception_info: tu.get_document_with_exception(second_doc_id) error_string = 'No document of type any found for id {0}'.format(str(second_doc_id)) assert error_string in str(exception_info.value)
def test_get_document_with_exception_throws_exception_when_document_of_specified_type_doesnt_exist( self): doc_id = uuid.uuid4() dict_in = {'_id': doc_id, 'type': 'twix'} tu.save_document(dict_in) with pytest.raises(NotFoundError) as exception_info: tu.get_document_with_exception(doc_id, document_type='skittles') error_string = 'No document of type skittles found for id {0}'.format( str(doc_id)) assert error_string in str(exception_info.value)
def test_get_document_with_exception_throws_exception_when_no_document_of_any_type_exists_for_requested_id( self): doc_id = uuid.uuid4() dict_in = {'_id': doc_id, 'type': 'twix'} tu.save_document(dict_in) second_doc_id = uuid.uuid4() with pytest.raises(NotFoundError) as exception_info: tu.get_document_with_exception(second_doc_id) error_string = 'No document of type any found for id {0}'.format( str(second_doc_id)) assert error_string in str(exception_info.value)
def get_image_paths_and_snap_id(img1_id_in, img2_id_in, img_id_out): img1_dict_in = get_document_with_exception(str(img1_id_in), 'picture') img1_filename_in = img1_dict_in['filename'] img2_dict_in = get_document_with_exception(str(img2_id_in), 'picture') img2_filename_in = img2_dict_in['filename'] img_filename_out = build_picture_name(img_id_out) pic1_path_in = build_picture_path(picture_name=img1_filename_in, snap_id=img1_dict_in['snap_id']) pic2_path_in = build_picture_path(picture_name=img2_filename_in, snap_id=img1_dict_in['snap_id']) pic_path_out = build_picture_path(picture_name=img_filename_out, snap_id=img1_dict_in['snap_id']) return {'img1_path': pic1_path_in, 'img2_path': pic2_path_in, 'img_out_path': pic_path_out, 'img_out_filename': img_filename_out, 'snap_id': img1_dict_in['snap_id']}
def call_edge_detect(image_id=None): ''' Invokes edge detection for a given image Accepts a GET parameter for detection threshold. Allowable values are 'all', 'auto', 'wide' and 'tight' ''' try: picture_dict = get_document_with_exception(image_id, document_type='picture') auto_id = uuid.uuid4() wide_id = uuid.uuid4() tight_id = uuid.uuid4() args_dict = gather_and_enforce_request_args([{'name': 'detection_threshold', 'default': 'all'}]) if args_dict['detection_threshold'] not in ['all', 'auto', 'wide', 'tight']: error_msg = 'invalid detection threshold specified. Allowable are all, auto, wide or tight' return Response(json.dumps(error_msg), status=409, mimetype='application/json') ans.edge_detect_task.delay(img_id_in=image_id, detection_threshold=args_dict['detection_threshold'], auto_id=auto_id, wide_id=wide_id, tight_id=tight_id) resp_json = {} if args_dict['detection_threshold'] in ['all', 'auto']: resp_json['auto_id'] = str(auto_id) if args_dict['detection_threshold'] in ['all', 'wide']: resp_json['wide_id'] = str(wide_id) if args_dict['detection_threshold'] in ['all', 'tight']: resp_json['tight_id'] = str(tight_id) return Response(json.dumps(resp_json), status=202, mimetype='application/json') except Exception as e: return Response(json.dumps(e.message), status=e.status_code, mimetype='application/json')
def distort_image_shepards(image_id_in=None, image_id_out=None, distortion_set_id=None): ''' Distorts an image using all the distortion pairs in a named distortion set It is necessary to call ImageMagick via command line to make this happen, no bindings in Pillow for this functionality :( Uses the Shepards algorithm for distortion ''' img_dict_in = get_document_with_exception(image_id_in, 'picture') group_id = img_dict_in['group_id'] img_filename_out = build_picture_name(image_id_out) pic_path_in = img_dict_in['uri'] pic_path_out = build_picture_path(picture_name=img_filename_out, snap_id=img_dict_in['snap_id']) command_string = build_command_string(distortion_set_id, pic_path_in, pic_path_out) os.system(command_string) img_dict_out = { '_id': str(image_id_out), 'type': 'picture', 'source': 'analysis', 'source_image_id': str(image_id_in), 'analysis_type': 'distort', 'group_id': group_id, 'snap_id': img_dict_in['snap_id'], 'filename': img_filename_out, 'uri': pic_path_out, 'created': str(datetime.datetime.now()) } save_generic(img_dict_out, 'picture')
def get_group_document(group_id): ''' Fetches the group document specified by the requested id ''' if group_id == 'current': settings_dict = get_settings_document() group_id = settings_dict['current_group_id'] return get_document_with_exception(group_id, document_type='group')
def edge_detect(img_id_in, detection_threshold='all', auto_id=uuid.uuid4(), wide_id=uuid.uuid4(), tight_id=uuid.uuid4()): pic_dict_in = get_document_with_exception(img_id_in, 'picture') if detection_threshold in ['all', 'auto']: edge_detect_auto(img_id_in, pic_dict_in, auto_id) if detection_threshold in ['all', 'wide'] and wide_id: edge_detect_with_canny_limits(img_id_in, pic_dict_in, wide_id, 10, 200) if detection_threshold in ['all', 'tight'] and tight_id: edge_detect_with_canny_limits(img_id_in, pic_dict_in, tight_id, 225, 250)
def get_group_document(group_id): """ Fetches the group document specified by the requested id """ if group_id == "current": settings_dict = get_settings_document() group_id = settings_dict["current_group_id"] return get_document_with_exception(group_id, document_type="group")
def get_image_paths_and_snap_id(img1_id_in, img2_id_in, img_id_out): img1_dict_in = get_document_with_exception(str(img1_id_in), 'picture') img1_filename_in = img1_dict_in['filename'] img2_dict_in = get_document_with_exception(str(img2_id_in), 'picture') img2_filename_in = img2_dict_in['filename'] img_filename_out = build_picture_name(img_id_out) pic1_path_in = build_picture_path(picture_name=img1_filename_in, snap_id=img1_dict_in['snap_id']) pic2_path_in = build_picture_path(picture_name=img2_filename_in, snap_id=img1_dict_in['snap_id']) pic_path_out = build_picture_path(picture_name=img_filename_out, snap_id=img1_dict_in['snap_id']) return { 'img1_path': pic1_path_in, 'img2_path': pic2_path_in, 'img_out_path': pic_path_out, 'img_out_filename': img_filename_out, 'snap_id': img1_dict_in['snap_id'] }
def generic_get_view(item_id='', document_type=''): ''' Retrieves an document for the item of the specified type for the supplied id ''' try: # raise exception if no item id is supplied # raise exception if no document type is supplied item_dict = get_document_with_exception(item_id, document_type) return Response(json.dumps(item_dict), status=200, mimetype='application/json') except Exception as e: # TODO add tests, bad paging info or strings that kill the map string could cause abends return Response(json.dumps(e.message), status=e.status_code, mimetype='application/json')
def clean_up_files(snap_id): ''' Cleans up files for the specified snap Defaults to current group (for determining types of files to delete), but can be overridden with a group_id GET parameter ''' try: snap_dict = get_document_with_exception(snap_id, document_type='snap') args_dict = gather_and_enforce_request_args([{'name': 'group_id', 'default': 'current'}]) clean_up_files_task.delay(snap_id, args_dict['group_id']) return Response(json.dumps(snap_dict), status=202, mimetype='application/json') except Exception as e: return Response(json.dumps(e.message), status=e.status_code, mimetype='application/json')
def call_distort_image(image_id=None): ''' Distorts an image according to the distortion pairs in the specified distortion set ''' try: new_image_id = uuid.uuid4() picture_dict = get_document_with_exception(image_id, document_type='picture') args_dict = gather_and_enforce_request_args([{'name': 'distortion_set_id', 'required': True}]) distortion_set_id = args_dict['distortion_set_id'] distortion_set_dict = get_document_with_exception(distortion_set_id, document_type='distortion_set') # TODO call this async via distort_image_shepards_task.delay as soon as it's working ans.distort_image_shepards(image_id_in=image_id, image_id_out=new_image_id, distortion_set_id=distortion_set_id) resp_json = { 'distorted_image_id': str(new_image_id) } return Response(json.dumps(resp_json), status=202, mimetype='application/json') except Exception as e: return Response(json.dumps(e.message), status=e.status_code, mimetype='application/json')
def generic_update_view(item_id='', document_type=''): try: item_dict = get_document_with_exception(item_id, document_type) if request.headers['Content-Type'] == 'application/json': for k in request.json.keys(): if doc_attribute_can_be_set(k): item_dict[k] = request.json[k] update_generic(item_dict, document_type) return Response(json.dumps(item_dict), status=200, mimetype='application/json') error_msg = 'problem with update: content type is not application/json' return Response(json.dumps(error_msg), status=409, mimetype='application/json') except Exception as e: return Response(json.dumps(e.message), status=e.status_code, mimetype='application/json')
def call_edge_detect(image_id=None): ''' Invokes edge detection for a given image Accepts a GET parameter for detection threshold. Allowable values are 'all', 'auto', 'wide' and 'tight' ''' try: picture_dict = get_document_with_exception(image_id, document_type='picture') auto_id = uuid.uuid4() wide_id = uuid.uuid4() tight_id = uuid.uuid4() args_dict = gather_and_enforce_request_args([{ 'name': 'detection_threshold', 'default': 'all' }]) if args_dict['detection_threshold'] not in [ 'all', 'auto', 'wide', 'tight' ]: error_msg = 'invalid detection threshold specified. Allowable are all, auto, wide or tight' return Response(json.dumps(error_msg), status=409, mimetype='application/json') ans.edge_detect_task.delay( img_id_in=image_id, detection_threshold=args_dict['detection_threshold'], auto_id=auto_id, wide_id=wide_id, tight_id=tight_id) resp_json = {} if args_dict['detection_threshold'] in ['all', 'auto']: resp_json['auto_id'] = str(auto_id) if args_dict['detection_threshold'] in ['all', 'wide']: resp_json['wide_id'] = str(wide_id) if args_dict['detection_threshold'] in ['all', 'tight']: resp_json['tight_id'] = str(tight_id) return Response(json.dumps(resp_json), status=202, mimetype='application/json') except Exception as e: return Response(json.dumps(e.message), status=e.status_code, mimetype='application/json')
def clean_up_files(snap_id): ''' Cleans up files for the specified snap Defaults to current group (for determining types of files to delete), but can be overridden with a group_id GET parameter ''' try: snap_dict = get_document_with_exception(snap_id, document_type='snap') args_dict = gather_and_enforce_request_args([{ 'name': 'group_id', 'default': 'current' }]) clean_up_files_task.delay(snap_id, args_dict['group_id']) return Response(json.dumps(snap_dict), status=202, mimetype='application/json') except Exception as e: return Response(json.dumps(e.message), status=e.status_code, mimetype='application/json')
def scale_image(img_id_in, img_id_out, group_id, **kwargs): # only works on black and white images for now # that should only be a problem for images that aren't of type 'L'. Add this test group_document = get_group_document(group_id) if 'scale_type' in kwargs: scale_type = kwargs['scale_type'] else: if 'scale_type' in group_document: scale_type = group_document['scale_type'] else: scale_type = 'colorize_bicubic' group_id = group_document['_id'] img_dict_in = get_document_with_exception(str(img_id_in), 'picture') img_filename_in = img_dict_in['filename'] img_filename_out = build_picture_name(img_id_out) pic_path_in = img_dict_in['uri'] pic_path_out = build_picture_path(picture_name=img_filename_out, snap_id=img_dict_in['snap_id']) image_in = Image.open(pic_path_in) image_scaled = scale_image_subtask(scale_type, image_in) image_scaled = blur_image(scale_type, image_scaled) image_colorized = colorize_image(scale_type, group_document, image_scaled) image_colorized.save(pic_path_out) img_dict_out = { '_id': str(img_id_out), 'type': 'picture', 'source': 'analysis', 'source_image_id': str(img_id_in), 'analysis_type': scale_type, 'group_id': group_id, 'snap_id': img_dict_in['snap_id'], 'filename': img_filename_out, 'uri': pic_path_out, 'created': str(datetime.datetime.now()) } save_generic(img_dict_out, 'picture')
def get_calibration_session_document(calibration_session_id): calibration_session_dict = get_document_with_exception( calibration_session_id, document_type='calibration_session') return calibration_session_dict
def build_blurred_cv2_image(img_id_in): pic_dict_in = get_document_with_exception(img_id_in, 'picture') image_in = cv2.imread(pic_dict_in['uri']) gray = cv2.cvtColor(image_in, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray, (3, 3), 0) return blurred
def get_distortion_pair_document(distortion_pair_id): distortion_pair_dict = get_document_with_exception(distortion_pair_id, document_type='distortion_pair') return distortion_pair_dict
def get_calibration_session_document(calibration_session_id): calibration_session_dict = get_document_with_exception(calibration_session_id, document_type='calibration_session') return calibration_session_dict
def get_distortion_pair_document(distortion_pair_id): distortion_pair_dict = get_document_with_exception( distortion_pair_id, document_type='distortion_pair') return distortion_pair_dict