def tailor(anno, src_path, dst_path): for box in anno['boxs']: if box["content"] == "Business_card": region = (box['x'], box['y'], box['x']+box['w'], box['y']+box['h']) crop(src_path, dst_path, region) return logger.error("unable to find annotation for business card in {0}".format(anno['img'])) raise ValueError
def crop_func(): session_id = request.args.get("session_id") filename = request.args.get("filename") path = app.config["UPLOAD_FOLDER"] + "/" + session_id + "/" + filename data = request.json X = int(data["X"]) Y = int(data["Y"]) xOffset = int(data["xOffset"]) yOffset = int(data["yOffset"]) crop(path, X, Y, xOffset, yOffset) return jsonify("OK"), 200
def crop_all_imgs(path, anno): title = os.path.basename(path) cropped_path = os.path.join(path, 'cropped') images = filter(lambda x: x.endswith('.png'), os.listdir(path)) if not os.path.exists(cropped_path): os.makedirs(cropped_path) from utils.crop import crop for frame in images: img_name = os.path.join(path, frame) dst_name = os.path.join(cropped_path, frame) region = anno[frame]['region'] # if os.path.exists(dst_name): # only process unfinished # continue try: crop(img_name, dst_name, region) except Exception as e: region_str = ','.join(map(lambda x: str(int((x))), region)) logger.error('error occured when cropping {0} with region {1}'.format(img_name, region_str))
model.add(Flatten()) elif(l.type == "CONNECTED"): model.add(Dense(l.output_size, weights=[l.weights,l.biases])) model.add(LeakyReLU(alpha=0.1)) else: print "Error: Unknown Layer Type" model.add(Activation('softmax')) return model def get_activations(model, layer, X_batch): get_activations = theano.function([model.layers[0].input], model.layers[layer].get_output(train=False), allow_input_downcast=True) activations = get_activations(X_batch) # same result as above return activations #image = readImg(os.path.join(os.getcwd(),'images/dog.file')) image = crop(os.path.join(os.getcwd(),'images/dog.jpg')) image = np.expand_dims(image, axis=0) googleNet = ReadGoogleNetWeights(os.path.join(os.getcwd(),'weights/extraction.weights')) #reshape weights in every layer for i in range(googleNet.layer_number): l = googleNet.layers[i] if(l.type == 'CONVOLUTIONAL'): weight_array = l.weights n = weight_array.shape[0] weight_array = weight_array.reshape((n//(l.size*l.size),(l.size*l.size)))[:,::-1].reshape((n,)) weight_array = np.reshape(weight_array,[l.n,l.c,l.size,l.size]) l.weights = weight_array if(l.type == 'CONNECTED'): weight_array = l.weights weight_array = np.reshape(weight_array,[l.output_size,l.input_size])
predictions = out[0] boxes = convert_yolo_detections(predictions) boxes = do_nms_sort(boxes,98) for i in range(98): for j in range(20): if(boxes[i].probs[j] != 0): print i,j print boxes[i].probs[j] draw_detections(os.path.join(os.getcwd(),'images/dog.jpg'),98,0.2,boxes,20,labels,'dog.jpg') ''' #for each image, we generate a detection result imagePath = os.path.join(os.getcwd(),'images') images = [f for f in listdir(imagePath) if isfile(join(imagePath, f))] for image_name in images: timer = Timer() image = crop(os.path.join(imagePath,image_name),resize_width=512,resize_height=512,new_width=448,new_height=448) image = np.expand_dims(image, axis=0) timer.tic() out = model.predict(image) timer.toc() print ('Total time is {:.3f}s ').format(timer.total_time) predictions = out[0] boxes = convert_yolo_detections(predictions) boxes = do_nms_sort(boxes,98) draw_detections(os.path.join(imagePath,image_name),98,0.2,boxes,20,labels,image_name) #draw_detections(os.path.join(os.getcwd(),'resized_images','1.jpg'),98,0.2,boxes,20,labels,image_name)
elif(l.type == "CONNECTED"): model.add(Dense(l.output_size, weights=[l.weights,l.biases])) model.add(LeakyReLU(alpha=0.1)) elif(l.type == "SOFTMAX"): model.add(Activation('softmax')) else: print "Error: Unknown Layer Type",l.type return model def get_activations(model, layer, X_batch): get_activations = theano.function([model.layers[0].input], model.layers[layer].get_output(train=False), allow_input_downcast=True) activations = get_activations(X_batch) # same result as above return activations #image = readImg(os.path.join(os.getcwd(),'images/dog.file')) image = crop(os.path.join(os.getcwd(),'images/eagle.jpg')) image = np.expand_dims(image, axis=0) darkNet = ReadDarkNetWeights(os.path.join(os.getcwd(),'weights/darknet.weights')) #reshape weights in every layer for i in range(darkNet.layer_number): l = darkNet.layers[i] if(l.type == 'CONVOLUTIONAL'): weight_array = l.weights n = weight_array.shape[0] weight_array = weight_array.reshape((n//(l.size*l.size),(l.size*l.size)))[:,::-1].reshape((n,)) weight_array = np.reshape(weight_array,[l.n,l.c,l.size,l.size]) l.weights = weight_array if(l.type == 'CONNECTED'): weight_array = l.weights weight_array = np.reshape(weight_array,[l.input_size,l.output_size])
for i in range(98): for j in range(20): if(boxes[i].probs[j] != 0): print i,j print boxes[i].probs[j] draw_detections(os.path.join(os.getcwd(),'images/dog.jpg'),98,0.2,boxes,20,labels,'dog.jpg') ''' #for each image, we generate a detection result imagePath = os.path.join(os.getcwd(), 'images') images = [f for f in listdir(imagePath) if isfile(join(imagePath, f))] for image_name in images: timer = Timer() image = crop(os.path.join(imagePath, image_name), resize_width=512, resize_height=512, new_width=448, new_height=448) image = np.expand_dims(image, axis=0) timer.tic() out = model.predict(image) timer.toc() print('Total time is {:.3f}s ').format(timer.total_time) predictions = out[0] boxes = convert_yolo_detections(predictions) boxes = do_nms_sort(boxes, 98) draw_detections(os.path.join(imagePath, image_name), 98, 0.2, boxes, 20, labels, image_name)
else: print "Error: Unknown Layer Type", l.type return model def get_activations(model, layer, X_batch): get_activations = theano.function( [model.layers[0].input], model.layers[layer].get_output(train=False), allow_input_downcast=True) activations = get_activations(X_batch) # same result as above return activations #image = readImg(os.path.join(os.getcwd(),'images/dog.file')) image = crop(os.path.join(os.getcwd(), 'images/eagle.jpg')) image = np.expand_dims(image, axis=0) darkNet = ReadDarkNetWeights( os.path.join(os.getcwd(), 'weights/darknet.weights')) #reshape weights in every layer for i in range(darkNet.layer_number): l = darkNet.layers[i] if (l.type == 'CONVOLUTIONAL'): weight_array = l.weights n = weight_array.shape[0] weight_array = weight_array.reshape( (n // (l.size * l.size), (l.size * l.size)))[:, ::-1].reshape( (n, )) weight_array = np.reshape(weight_array, [l.n, l.c, l.size, l.size]) l.weights = weight_array
print "Error: Unknown Layer Type" model.add(Activation('softmax')) return model def get_activations(model, layer, X_batch): get_activations = theano.function( [model.layers[0].input], model.layers[layer].get_output(train=False), allow_input_downcast=True) activations = get_activations(X_batch) # same result as above return activations #image = readImg(os.path.join(os.getcwd(),'images/dog.file')) image = crop(os.path.join(os.getcwd(), 'images/dog.jpg')) image = np.expand_dims(image, axis=0) googleNet = ReadGoogleNetWeights( os.path.join(os.getcwd(), 'weights/extraction.weights')) #reshape weights in every layer for i in range(googleNet.layer_number): l = googleNet.layers[i] if (l.type == 'CONVOLUTIONAL'): weight_array = l.weights n = weight_array.shape[0] weight_array = weight_array.reshape( (n // (l.size * l.size), (l.size * l.size)))[:, ::-1].reshape( (n, )) weight_array = np.reshape(weight_array, [l.n, l.c, l.size, l.size]) l.weights = weight_array