def detect(self, image): detections = self.net.detect_on_image(image, self.target_size, self.device, is_pad=False, keep_thresh=self.conf_thresh) bboxs = get_detections(detections, self.conf_thresh) return [0, bboxs, 0]
def plot_risk(df_risk, real_outbreaks, shocks, ax): # TODO:add date and not only the #month risk = df_risk['risk'] detections = utils.get_detections(risk, RISK_THRESH) ax.plot(risk, lw=3) ax.axhline(RISK_THRESH, c='gray', ls='--') for i, d in enumerate(detections): ax.axvline(x=d, lw=2, alpha=0.5, c='y', label="detections" if i == 0 else None) try: ax.fill_between(range(d, d+DETECTION_THRESH), risk[d:d+DETECTION_THRESH], alpha=0.5, color='y') except: ax.fill_between(range(d, len(risk)), risk[d:len(risk)], alpha=0.5, color='y') for i, r in enumerate(real_outbreaks): ax.axvline(x=r, c='r', label="outbreaks" if i == 0 else None) for i, s in enumerate(shocks): ax.axvline(x=s, c='g', label="shocks" if i == 0 else None) ax.set_ylabel('risk') ax.set_xlabel('month') ax.set_ylim(0, 1) handles, _ = ax.get_legend_handles_labels() patch = mpatches.Patch(color='y', alpha=0.5, label='Detection window') handles.append(patch) ax.legend(handles=handles, loc='upper center')
target_size = (800, 800) net = SSD("test") net.load_state_dict(torch.load('weights/WIDERFace_DSFD_RES152.pth')) net.to(device).eval() img_path = '/home/fer/Videos/YDXJ2900.jpg' img = cv2.imread(img_path, cv2.IMREAD_COLOR) t1 = time.time() detections = net.detect_on_image(img, target_size, device, is_pad=False, keep_thresh=conf_thresh) t2 = time.time() print("time: {:.2f}".format(t2 - t1)) t1 = time.time() detections = net.detect_on_image(img, target_size, device, is_pad=False, keep_thresh=conf_thresh) t2 = time.time() print("time: {:.2f}".format(t2 - t1)) vis_detections(img, detections, conf_thresh, show_text=False) bboxs = get_detections(detections, conf_thresh) print(bboxs)
def index(): print('Request-form', list(request.form.keys()), file=sys.stderr) print('Request-form-name', request.form['name'], file=sys.stderr) # print('Request-form-image',request.form['image'],file=sys.stderr) image_name = request.form['name'] image_string = request.form['image'] #image_bytes = bytes(image_string,'utf-8') #image_decoded = base64.decodestring(image_string) image = Image.open(BytesIO(base64.b64decode(image_string))) # rotated_image = image.rotate(270,expand=True) # input_array = np.array(rotated_image) input_array = np.array(image) input_array = np.expand_dims(input_array, axis=0) #result_array = detect.run(input_array) (_boxes, _scores, _classes, _masks) = sess.run([boxes, scores, classes, masks], feed_dict={input_: input_array}) _boxes = np.squeeze(_boxes, axis=0) _scores = np.squeeze(_scores, axis=0) _classes = np.squeeze(_classes, axis=0) _masks = np.squeeze(_masks, axis=0) input_array = np.squeeze(input_array, axis=0) detections = utils.get_detections(_scores, config.threshold_score) utils.draw_bounding_box(input_array, detections, _boxes, _classes, class_map, _masks) result_image = Image.fromarray(input_array) #print('rotated_image.shape = ',input_array.shape) result_image.save('output.jpg', format='JPEG') #convert image back to string.. buffered = BytesIO() result_image.save(buffered, format="JPEG") final_img_str = base64.b64encode(buffered.getvalue()) # print('Request-files:',request.files,file=sys.stderr) # print('Requestfiletype:',type(request.files),file=sys.stderr) # data = request.files.to_dict() # print('data',data,file=sys.stderr) # #to-do Input file validation... (ensure input file is valid jpg or png) # file = data['upload'] # print('File name:',file.filename,file=sys.stderr) # file_path = os.path.join("Images",file.filename) # file.save(file_path) # print('File saved with name:',file.filename,file=sys.stderr) #Deserialize the image.. # with open(image_name,'wb') as image_file: # image_file.write(image) response = final_img_str # print("Returning Image Response...",file=sys.stderr) return response
def index(): print('Request-form', list(request.form.keys()), file=sys.stderr) #print('Request-form-name',request.form['name'],file=sys.stderr) # print('Request-form-image',request.form['image'],file=sys.stderr) #image_name = request.form['name'] image_string = request.form['image'] #image_bytes = bytes(image_string,'utf-8') #image_decoded = base64.decodestring(image_string) image = Image.open(BytesIO(base64.b64decode(image_string))) # rotated_image = image.rotate(270,expand=True) # input_array = np.array(rotated_image) input_array = np.array(image) input_array = np.expand_dims(input_array, axis=0) detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(config.mask_model_infer_path, mode='rb') as graph_file: serialized_graph = graph_file.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def) class_map = utils.get_class_map(config.class_map_file) with tf.Session(graph=detection_graph) as sess: input_ = sess.graph.get_tensor_by_name("import/image_tensor:0") boxes = sess.graph.get_tensor_by_name("import/detection_boxes:0") scores = sess.graph.get_tensor_by_name("import/detection_scores:0") classes = sess.graph.get_tensor_by_name("import/detection_classes:0") masks = sess.graph.get_tensor_by_name("import/detection_masks:0") #result_array = detect.run(input_array) (_boxes, _scores, _classes, _masks) = sess.run([boxes, scores, classes, masks], feed_dict={input_: input_array}) _boxes = np.squeeze(_boxes, axis=0) _scores = np.squeeze(_scores, axis=0) _classes = np.squeeze(_classes, axis=0) _masks = np.squeeze(_masks, axis=0) input_array = np.squeeze(input_array, axis=0) detections = utils.get_detections(_scores, config.threshold_score) utils.draw_bounding_box(input_array, detections, _boxes, _classes, class_map, _masks) result_image = Image.fromarray(input_array) #print('rotated_image.shape = ',input_array.shape) #result_image.save('output.jpg',format='JPEG') #convert image back to string.. buffered = BytesIO() result_image.save(buffered, format="JPEG") final_img_str = base64.b64encode(buffered.getvalue()) # print('Request-files:',request.files,file=sys.stderr) # print('Requestfiletype:',type(request.files),file=sys.stderr) # data = request.files.to_dict() # print('data',data,file=sys.stderr) # #to-do Input file validation... (ensure input file is valid jpg or png) # file = data['upload'] # print('File name:',file.filename,file=sys.stderr) # file_path = os.path.join("Images",file.filename) # file.save(file_path) # print('File saved with name:',file.filename,file=sys.stderr) #Deserialize the image.. # with open(image_name,'wb') as image_file: # image_file.write(image) response = final_img_str # print("Returning Image Response...",file=sys.stderr) # tf.reset_defualt_graph(); return response