class Predict(Resource): model_wrapper = ModelWrapper() @api.doc('predict') @api.expect(input_parser) def post(self): """Make a prediction given input data""" result = {'status': 'error'} args = input_parser.parse_args() input_data = args['image'].read() input_encoded = base64.urlsafe_b64encode(input_data) input_array = np.array(input_encoded) input_instance = np.expand_dims(input_array, axis=0) image = self.model_wrapper.predict(input_instance) response = make_response(image) response.headers.set('Content-Type', 'image/png') response.headers.set('Content-Disposition', 'attachment', filename='result.png') result['status'] = 'ok' return response
class Predict(Resource): model_wrapper = ModelWrapper() @api.doc('predict') @api.expect(video_parser) @api.marshal_with(predict_response) def post(self): """Make a prediction given input data""" result = {'status': 'error'} # Take video save it into directory args = video_parser.parse_args() video_data = args['video'] filepath = os.path.join(UPLOAD_FOLDER, video_data.filename) video_data.save(filepath) # Run predict function on file preds = self.model_wrapper.predict(filepath) label_preds = [{'label_id': p[0], 'label': p[1], 'probability': p[2]} for p in [x for x in preds]] result['predictions'] = label_preds result['status'] = 'ok' return result
class Predict(Resource): model_wrapper = ModelWrapper() @api.doc('predict') @api.expect(image_parser) @api.marshal_with(predict_response) def post(self): '''Make a prediction given input data''' result = {'status': 'error'} args = image_parser.parse_args() if not args['image'].mimetype.endswith(('jpg', 'jpeg', 'png')): abort( 400, 'Invalid file type/extension. Please provide an image in JPEG or PNG format.' ) image_data = args['image'].read() preds = self.model_wrapper.predict(image_data) label_preds = [{ 'index': p[0], 'caption': p[1], 'probability': p[2] } for p in [x for x in preds]] result['predictions'] = label_preds result['status'] = 'ok' return result
class Predict(Resource): model_wrapper = ModelWrapper() @api.doc('predict') @api.expect(image_parser) @api.marshal_with(predict_response) def post(self): """Make a prediction given input data""" result = {'status': 'error'} args = image_parser.parse_args() image_data = args['image'].read() image = read_image(image_data) preds = self.model_wrapper.predict(image) label_preds = [{ 'label_id': p[0], 'label': p[1], 'probability': p[2] } for p in [x for x in preds]] result['predictions'] = label_preds result['status'] = 'ok' return result
class Predict(Resource): model_wrapper = ModelWrapper() @api.doc('predict') @api.expect(input_parser) @api.marshal_with(predict_response) def post(self): """Make a prediction given input data""" result = {'status': 'error'} args = input_parser.parse_args() try: input_data = args['file'].read() image = read_image(input_data) except OSError as e: abort(400, "Please submit a valid image in PNG, Tiff or JPEG format") label_preds = self.model_wrapper.predict(image) result['predictions'] = label_preds result['status'] = 'ok' return result
class Predict(Resource): model_wrapper = ModelWrapper() @api.doc('predict') @api.expect(model_input) @api.marshal_with(predict_response) def post(self): '''Make a prediction given input data''' result = {'status': 'error'} j = request.get_json() seed_text = j['seed_text'] gen_chars = j['chars'] if 'chars' in j else DEFAULT_CHARS generated_text = self.model_wrapper.predict(seed_text, gen_chars) full_text = seed_text + generated_text model_pred = { 'seed_text': seed_text, 'generated_text': generated_text, 'full_text': full_text } result['prediction'] = model_pred result['status'] = 'ok' return result
class Predict(Resource): model_wrapper = ModelWrapper() @api.doc('predict') @api.expect(input_parser) @api.marshal_with(predict_response) def post(self): """Make a prediction given input data""" result = {'status': 'error'} args = input_parser.parse_args() input_data = args['INPUT_NAME'].read() preds = self.model_wrapper.predict(input_data) # Modify this code if the schema is changed label_preds = [{ 'label_id': p[0], 'label': p[1], 'probability': p[2] } for p in [x for x in preds]] result['predictions'] = label_preds result['status'] = 'ok' return result
class Predict(Resource): model_wrapper = ModelWrapper() @api.doc('predict') @api.expect(input_parser) @api.marshal_with(predict_response) def post(self): """Make a prediction given input data""" result = {'status': 'error'} args = input_parser.parse_args() input_data = args['image'].read() stillimg = read_still_image(input_data) preds = self.model_wrapper.predict(stillimg) label_preds = [] for res in preds: label_preds.append({ 'age_estimation': res[0]['age'], 'face_box': res[0]['box'] }) result['predictions'] = label_preds result['status'] = 'ok' return result
class Predict(Resource): model_wrapper = ModelWrapper() @api.doc(produces=['audio/wav']) @api.expect(input_parser) def get(self): """Make a prediction given input data""" args = input_parser.parse_args() model = args['model'] preds = self.model_wrapper.predict(model) response = make_response(open('output.wav', 'rb').read()) response.headers.set('Content-Type', 'audio/wav') response.headers.set('Content-Disposition', 'attachment', filename='result.wav') return response
class Predict(Resource): mw = ModelWrapper() @api.doc('predict') @api.expect(audio_parser) @api.marshal_with(predict_response) def post(self): """Predict audio classes from input data""" result = {'status': 'error'} args = audio_parser.parse_args() audio_data = args['audio'].read() # clean up from earlier runs if os.path.exists("/audio.wav"): os.remove("/audio.wav") if '.wav' in str(args['audio']): file = open("/audio.wav", "wb") file.write(audio_data) file.close() else: e = BadRequest() e.data = {'status': 'error', 'message': 'Invalid file type/extension'} raise e # Getting the predictions try: preds = self.mw.predict("/audio.wav", args['start_time']) except ValueError: e = BadRequest() e.data = {'status': 'error', 'message': 'Invalid start time: value outside audio clip'} raise e # Aligning the predictions to the required API format label_preds = [{'label_id': p[0], 'label': p[1], 'probability': p[2]} for p in preds] result['predictions'] = label_preds result['status'] = 'ok' os.remove("/audio.wav") return result
class Predict(Resource): model_wrapper = ModelWrapper() @api.doc('predict') @api.expect(image_parser) @api.marshal_with(predict_response) def post(self): '''Make a prediction given input data''' result = {'status': 'error'} args = image_parser.parse_args() image_data = args['image'].read() image = read_image(image_data) label_preds = self.model_wrapper.predict(image) result['predictions'] = label_preds result['status'] = 'ok' return result
class Predict(Resource): model_wrapper = ModelWrapper() @api.doc('predict') @api.expect(image_parser) @api.marshal_with(predict_response) def post(self): """Make a prediction given input data""" result = {'status': 'error'} args = image_parser.parse_args() image_data = args['image'].read() try: image = read_image(image_data) preds = self.model_wrapper.predict(image) label_preds = [{'probability': float(preds)}] result['predictions'] = label_preds result['status'] = 'ok' except ValueError as e: abort(400, str(e)) return result
class Predict(Resource): model_wrapper = ModelWrapper() @api.doc('predict') @api.expect(input_parser) @api.marshal_with(predict_response) def post(self): """Make a prediction given input data""" result = {'status': 'error'} args = input_parser.parse_args() input_data = args['file'] model = args['model'] input_array = load_array(input_data) preds = self.model_wrapper.predict(input_array, model) result['predictions'] = preds result['status'] = 'ok' return result
class Predict(Resource): model_wrapper = ModelWrapper() @api.doc('predict') @api.expect(image_parser) @api.marshal_with(predict_response) def post(self): """Make a prediction given input data""" result = {'status': 'error'} args = image_parser.parse_args() image_data = args['image'].read() image = read_image(image_data) resized_im, seg_map = self.model_wrapper.predict(image) result['image_size'] = resized_im.size result['seg_map'] = seg_map result['status'] = 'ok' return result
class Predict(Resource): mw = ModelWrapper() @api.doc('predict') @api.expect(audio_parser) @api.marshal_with(predict_response) def post(self): """Generate audio embedding from input data""" result = {'status': 'error'} args = audio_parser.parse_args() audio_data = args['audio'].read() # clean up from earlier runs if os.path.exists("/audio.wav"): os.remove("/audio.wav") if '.wav' in str(args['audio']): file = open("/audio.wav", "wb") file.write(audio_data) file.close() else: e = BadRequest() e.data = {'status': 'error', 'message': 'Invalid file type/extension'} raise e # Getting the predictions preds = self.mw.predict("/audio.wav") # Aligning the predictions to the required API format result['embedding'] = preds.tolist() result['status'] = 'ok' os.remove("/audio.wav") return result
class Predict(Resource): mw = ModelWrapper() @api.doc('predict') @api.expect(audio_parser) @api.marshal_with(predict_response) def post(self): """Predict audio classes from input data""" result = {'status': 'error'} args = audio_parser.parse_args() audio_data = args['audio'].read() # clean up from earlier runs if os.path.exists("/audio.wav"): os.remove("/audio.wav") file = open("/audio.wav", "wb") file.write(audio_data) file.close() #Getting the predicions preds = self.mw.predict("/audio.wav") #Aligning the predictions to the required API format label_preds = [{ 'label_id': p[0], 'label': p[1], 'probability': p[2] } for p in preds] result['predictions'] = label_preds result['status'] = 'ok' os.remove("/audio.wav") return result
class Predict(Resource): model_wrapper = ModelWrapper() @api.doc('predict') @api.expect(input_parser) def post(self): """Make a prediction given input data""" result = {'status': 'error'} args = input_parser.parse_args() if not args['file'].mimetype.endswith(('jpg', 'jpeg', 'png')): abort( 400, 'Invalid file type/extension. Please provide an image in JPEG or PNG format.' ) image_input_read = Image.open(args['file']) image_mask_type = args['mask_type'] # creating directory for storing input input_directory = '/workspace/assets/input/file' if not os.path.exists(input_directory): os.mkdir(input_directory) # clear input directory input_folder = '/workspace/assets/input/file/' for file in glob.glob(input_folder + '*'): try: try: os.remove(file) except: shutil.rmtree(file) except: continue # save input image image_input_read = image_input_read.convert('RGB') image_input_read.save('/workspace/assets/input/file/input.jpg') # face detection, alignment and resize using openface args = { 'inputDir': input_directory, 'outputDir': '/workspace/assets/input/file/align', 'landmarks': 'innerEyesAndBottomLip', 'dlibFacePredictor': '/workspace/openface/models/dlib/shape_predictor_68_face_landmarks.dat', 'verbose': True, 'size': 64, 'skipMulti': False, 'fallbackLfw': None, 'mode': 'align' } try: coordinates = alignMain(args) coordinates_string = str(coordinates) pattern = '^\s*\[\s*\(\s*(\d+)\s*,\s*(\d+)\s*\)\s*\(\s*(\d+)\s*,\s*(\d+)\s*\)\s*\]\s*$' m = re.match(pattern, coordinates_string) if m: final_coordinates = '[[{},{}],[{},{}]]'.format( m.group(1), m.group(2), m.group(3), m.group(4)) except: #abort if there face is not detected abort(400, 'No face was detected in the image.') # store aligned input input_data = '/workspace/assets/input/file/align/file/input.png' # image_path = self.model_wrapper.predict(input_data, image_mask_type) """ preparing image collage """ new_collage_path = "/workspace/assets/center_mask/completed/Collage.jpg" img_columns = 5 img_rows = 4 img_width = 320 img_height = 256 thumbnail_width = img_width // img_columns thumbnail_height = img_height // img_rows size = thumbnail_width, thumbnail_height new_collage = Image.new('RGB', (img_width, img_height)) images_list = [] filenames = [] for filename in glob.glob(image_path): filenames.append(filename) filenames.sort() for i in filenames: im = Image.open(i) im.thumbnail(size) images_list.append(im) i = 0 x = 0 y = 0 for col in range(img_columns): for row in range(img_rows): new_collage.paste(images_list[i], (x, y)) i += 1 y += thumbnail_height x += thumbnail_width y = 0 new_collage.save(new_collage_path) """ end of collage creation process """ img = Image.open(new_collage_path, mode='r') imgByteArr = io.BytesIO() img.save(imgByteArr, format='JPEG') imgByteArr = imgByteArr.getvalue() response = make_response(imgByteArr) response.headers.set('Content-Type', 'image/jpeg') response.headers.set('Content-Disposition', 'attachment', filename='result.jpg') response.headers.set('coordinates', final_coordinates) return response
'id': fields.String(required=True, description='Model identifier'), 'name': fields.String(required=True, description='Model name'), 'description': fields.String(required=True, description='Model description'), 'license': fields.String(required=False, description='Model license') }) @api.route('/metadata') class Model(Resource): @api.doc('get_metadata') @api.marshal_with(model_meta) def get(self): '''Return the metadata associated with the model''' return MODEL_META_DATA model_wrapper = ModelWrapper() model_label = api.model('ModelLabel', { 'id': fields.String(required=True, description='Class label identifier'), 'name': fields.String(required=True, description='Class label'), }) labels_response = api.model('LabelsResponse', { 'count': fields.Integer(required=True, description='Number of class labels returned'), 'labels': fields.List(fields.Nested(model_label), description='Class labels that can be predicted by the model') }) @api.route('/labels') class Labels(Resource): @api.doc('get_labels') @api.marshal_with(labels_response)
from core.backend import ModelWrapper import cv2 model_wrapper = ModelWrapper() img = cv2.imread('input.jpg') image_data = cv2.imencode('.jpg', img)[1].tostring() # with open('input.jpg', 'rb') as f: # image_data = f.read() preds = model_wrapper.predict(image_data) label_preds = [{ 'index': p[0], 'caption': p[1], 'probability': p[2] } for p in preds] print(label_preds)