def read_inputs(self): if self.mode == 'inference': image_feed = tf.placeholder(dtype=tf.string, shape=[], name="image_feed") input_feed = tf.placeholder(dtype=tf.int64, shape=[None], name="input_feed") images = tf.expand_dims( image_processing.process_image(image_feed, is_training=False, height=299, width=299, thread_id=0, image_format='jpeg'), 0) input_seqs = tf.expand_dims(input_feed, 1) target_seqs = None input_mask = None else: input_queue = inputs_ops.prefetch_input_data( self.reader, file_pattern=self.input_file_pattern, # Must be given as input is_training=True, batch_size=32, values_per_shard=2300, input_queue_capacity_factor=2, num_reader_threads=1) images_and_captions = [] for thread_id in range(4): serialized_sequence_example = input_queue.dequeue() encoded_image, caption = inputs_ops.parse_sequence_example( serialized_sequence_example, image_feature="image/data", caption_feature="image/caption_ids") image = image_processing.process_image(encoded_image, is_training=False, height=299, width=299, thread_id=thread_id, image_format="jpeg") images_and_captions.append([image, caption]) queue_capacity = (2 * 4 * self.batch_size) images, input_seqs, target_seqs, input_mask = ( inputs_ops.batch_with_dynamic_pad( images_and_captions, batch_size=32, queue_capacity=queue_capacity)) self.images = images self.input_seqs = input_seqs self.target_seqs = target_seqs self.input_mask = input_mask
def process_corner(color_image,n_edge): thres_image = process_image(color_image,do_threshold=False) thres_edges, thres_cnts, thres_lines = edge_find(thres_image,130,200,250,dilate=1) scanned_lines = 0 distances = 0 #thres_edges = cv2.erode(thres_edges,kernel,1) #averaged_thres_lines = average_over_nearby_lines(thres_lines) selected_lines = select_corner_lines(thres_lines) averaged_thres_lines = average_over_nearby_lines(selected_lines) selected_lines_img = deepcopy(color_image) all_lines_img = deepcopy(color_image) lines_img = deepcopy(color_image) if thres_lines: for l in thres_lines: cv2.line(all_lines_img,(l.x1,l.y1),(l.x2,l.y2),(0,0,255),2,cv2.LINE_AA) if selected_lines: for l in selected_lines: cv2.line(selected_lines_img,(l.x1,l.y1),(l.x2,l.y2),(0,0,255),2,cv2.LINE_AA) if averaged_thres_lines: for l in averaged_thres_lines: cv2.line(lines_img,(l.x1,l.y1),(l.x2,l.y2),(0,0,255),2,cv2.LINE_AA) if(len(averaged_thres_lines) > 0): scanned_lines = averaged_thres_lines distances = [] for l in averaged_thres_lines: distances.append( ( (l.x1+l.x2)/2, (l.y1+l.y2)/2 ) ) if scanned_lines: for l in scanned_lines: cv2.line(lines_img,(l.x1,l.y1),(l.x2,l.y2),(255,0,0),2,cv2.LINE_AA) return thres_edges, lines_img, all_lines_img, averaged_thres_lines, scanned_lines, distances
def img_proccessor(): while True: last_time = time.time() #20/0 65/5 190 monitor = {'top': 190, 'left': 0, 'width': 640, 'height': 360} original_img = np.array(sct.grab(monitor)) #resized_img = cv2.resize(original_img, (640, 360)) try: fps_calc = 1.0 / (time.time() - last_time) except: pass #print(fps_calc) fps = ('fps: {0}'.format(int(fps_calc))) new_img = cv2.putText(process_image(original_img), fps, (522, 40), fontFace=4, color=[18, 153, 255], thickness=1, fontScale=0.8) #Color = BGR cv2.imshow('Processed', new_img) # FPS Monitor #print('fps: {0}'.format(1 / (time.time()-last_time))) if cv2.waitKey(1) & 0xFF == ord('q'): cv2.destroyAllWindows() break
def HackTest(): refImageStream = get_image_from_url(r'https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc6/211085_364348883660343_553562280_n.jpg') cpntImageStreams = LoadCpntImages() mosaic = process_image(refImageStream, cpntImageStreams) outFile = open("hack.jpg", "wb") outFile.write(mosaic) outFile.close()
def FacebookTest(): refImageStream = get_image_from_url(r'http://blogs-images.forbes.com/jonbruner/files/2011/07/facebook_logo.jpg') cpntImageStreams = LoadCpntImages() mosaic = process_image(refImageStream, cpntImageStreams) outFile = open("facebook.jpg", "wb") outFile.write(mosaic) outFile.close()
def Test(): refImageStream = LoadRefImage() cpntImageStreams = LoadCpntImages() mosaic = process_image(refImageStream, cpntImageStreams) outFile = open(MosaicFilename+".jpg", "wb") outFile.write(mosaic) outFile.close()
def process_image() -> str: """Transforms an image to text and returns the the text. After processing the image is deleted. Returns: str: The textual representation of an image. """ # Check if file is sent if "file" not in request.files: return "No file found!" file = request.files["file"] # Check for empty filename if file.filename == "": return "Empty filename!" # Check if file type is valid if not allowed_file(filename=file.filename, allowed_extensions=ALLOWED_EXTENSIONS): return "Invalid file type!" if file: filename = secure_filename(file.filename) file.save(os.path.join(app.config["UPLOAD_FOLDER"], filename)) # Process image image_text = image_processing.process_image("{}/{}".format( app.config["UPLOAD_FOLDER"], filename)) os.remove(os.path.join(app.config["UPLOAD_FOLDER"], filename)) return image_text
def handle_photo_msg(message): try: if not os.path.exists(storage_path): os.makedirs(storage_path, exist_ok=True) image_id = message.photo[-1].file_id image_name = "{0}.jpg".format(image_id) logger.info("Got image from {0} --- Image: {1}\n".format(message.chat.username, image_name)) image_info = bot.get_file(image_id) downloaded_image = bot.download_file(image_info.file_path) image = open("{0}/{1}".format(storage_path, image_name), 'wb') image.write(downloaded_image) image.close() logger.info("Starting image processing. --- Image: {0}\n".format(image_name)) out = process_image(image_name) bot.send_message(chat_id=message.chat.id, text=out, reply_to_message_id=message.message_id) except Exception as e: logger.error("ExceptionFromPhoto {0}: ".format(image_name) + str(e)) pass
def predict(image_path, model, topk=5): ''' Predict the class (or classes) of an image using a trained deep learning model. ''' # TODO: Implement the code to predict the class from an image file processed_img = ip.process_image(image_path) ts_im = torch.from_numpy(processed_img).float() ts_im = ts_im.unsqueeze(0) model.to(device) with torch.no_grad(): outputs = model.forward(ts_im.to(device)) results = np.exp(outputs).topk(topk) class_to_idx_dic = { key: value for value, key in model.class_to_idx.items() } probs = results[0].data.cpu().numpy()[0] classes = results[1].data.cpu().numpy()[0] classes = [class_to_idx_dic[classes[i]] for i in range(classes.size)] print(probs) print(classes) return probs, classes
def test_input_fn(): import image_processing import urllib.request import tensorflow.contrib.data as tfdata import image_embedding if args.test_urls: jpegs = [ urllib.request.urlopen(url).read() for url in args.test_urls.split(',') ] with tf.Graph().as_default() as g: jpeg = tf.placeholder(dtype=tf.string) image = image_processing.process_image(jpeg, False) features = image_embedding.inception_v3([image], False, False) saver = tf.train.Saver( tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope="InceptionV3")) with tf.Session(graph=g) as sess: saver.restore(sess, args.cnn_model) features_list = [ sess.run(features, feed_dict={jpeg: j}) for j in jpegs ] dataset = tfdata.Dataset.from_tensor_slices(np.array(features_list)) return {'features': dataset.make_one_shot_iterator().get_next()}, None else: raise Exception('pass test_urls')
def process_more_outputs(color_image,n_edge): thres_image = process_image(color_image,do_threshold=True,thres_val=90) med_val=np.median(thres_image) lower=int(max(0 ,0.7*med_val)) upper=int(min(255,1.3*med_val)) #lower = 50 #upper = 100 thres_edges, thres_cnts, thres_lines = edge_find(thres_image,lower,upper,250,dilate=1) scanned_lines = 0 distances = 0 #thres_edges = cv2.erode(thres_edges,kernel,1) #averaged_thres_lines = average_over_nearby_lines(thres_lines) selected_outer_lines = select_outer_lines(thres_lines,n_edge) inner_thres_image = process_image(color_image,do_threshold=True,thres_val=130) med_val=np.median(inner_thres_image) lower=int(max(0 ,0.7*med_val)) upper=int(min(255,1.3*med_val)) #lower = 50 #upper = 100 inner_thres_edges, inner_thres_cnts, inner_thres_lines = edge_find(inner_thres_image,lower,upper,250,dilate=1) selected_inner_lines = select_inner_lines(inner_thres_lines,thres_lines,n_edge) selected_lines = selected_inner_lines+selected_outer_lines averaged_thres_lines = average_over_nearby_lines(selected_lines) selected_lines_img = deepcopy(color_image) all_lines_img = deepcopy(color_image) lines_img = deepcopy(color_image) if thres_lines: for l in thres_lines: cv2.line(all_lines_img,(l.x1,l.y1),(l.x2,l.y2),(0,0,255),2,cv2.LINE_AA) if selected_lines: for l in selected_lines: cv2.line(selected_lines_img,(l.x1,l.y1),(l.x2,l.y2),(0,0,255),2,cv2.LINE_AA) if averaged_thres_lines: for l in averaged_thres_lines: cv2.line(lines_img,(l.x1,l.y1),(l.x2,l.y2),(0,0,255),2,cv2.LINE_AA) if(len(averaged_thres_lines) > 1): scanned_lines,distances = distance_between_lines(averaged_thres_lines[0],averaged_thres_lines[1],vertical=True) if scanned_lines: for l in scanned_lines: cv2.line(lines_img,(l.x1,l.y1),(l.x2,l.y2),(255,0,0),2,cv2.LINE_AA) return thres_edges, lines_img, thres_image, averaged_thres_lines, scanned_lines, distances,all_lines_img,selected_lines_img
def build_preprocess(self): """Build the image preprocessing ops.""" with tf.name_scope("preprocessing"): images = tf.expand_dims( image_processing.process_image( self.images, distort=self.config.distort_image), 0) self.images = images
def post(self): api = facebook.GraphAPI(self.current_user["access_token"]) reference = self.request.files["pic"][0]["body"] components = get_photo_array(api, maxPhotos = 300) self.set_header("Content-Type", "image/jpg") result = process_image(reference, components) self.write(result)
def post(self): #self.write("Processing...") api = facebook.GraphAPI(self.current_user["access_token"]) reference = urllib2.urlopen(self.get_argument("src")).read() components = get_photo_array(api, maxPhotos = 300) self.set_header("Content-Type", "image/jpg") result = process_image(reference, components) self.write(result)
def predict_steering_angle(model, img_name): """ Uses a model to predict the steering angle of a given image. param: model: the model to check param: img_name: the image file name """ img = cv2.imread(img_name) transformed_image_array = process_image(img)[None, :, :, :] return float(model.predict(transformed_image_array, batch_size=1))
def get_sudoku_and_filename(img): """ Creates a GIF highlighting the digits found Returns a 9*9 array of the identified digits the filename """ # Generate a unique filename filename = uuid.uuid4().hex[:3] fixed_perspective = image_processing.process_image(img) if fixed_perspective is None: return None, filename cv2.imwrite(sudoku_dir + filename + '_unsolved.jpg', fixed_perspective) fixed_perspective_gray = cv2.cvtColor(fixed_perspective, cv2.COLOR_BGR2GRAY) # Crop each box and pass its contents to get_value_from_image() # Add results to the matrix array matrix = [] x = y = 0 size = fixed_perspective.shape[0] // 9 for i in range(1, 10): for j in range(1, 10): cell = fixed_perspective_gray[y + (size * (i - 1)):y + (size * i + 1), x + (size * (j - 1)):x + (size * j + 1)] cropped = cell[cell.shape[0] // 5:cell.shape[1] - cell.shape[0] // 5, cell.shape[0] // 5:cell.shape[0] - (cell.shape[0] // 5)] value = image_processing.get_value_from_image( cropped, cached_sudoku_templates, (38, 57), threshold=0.435) matrix.append(value) if value is not 0: cv2.putText(fixed_perspective, value, (x + size * j - size // 4 - 27, y + (size * i + 1) - 13), cv2.FONT_HERSHEY_SIMPLEX, fontScale, (255, 100, 0), thickness, 2) # Output the detected_digits to file cv2.imwrite(sudoku_dir + filename + '_detected.jpg', fixed_perspective) # Create a GIF of the detected digits and the unsolved sudoku frames = [ sudoku_dir + filename + '_unsolved.jpg', sudoku_dir + filename + '_detected.jpg' ] image_sequence = mpy.ImageSequenceClip(frames, fps=2) image_sequence.write_gif(sudoku_dir + filename + '.gif', fps=2) rows = [] for i in range(0, 81, 9): rows.append(matrix[i:i + 9]) return rows, filename
def create_captions_records(records_path, image_records, captions, vocabulary, cnn_model): dataset = contrib.data.TFRecordDataset([image_records]) dataset = dataset.map(parse_image) imgage_record = dataset.make_one_shot_iterator().get_next() img_jpeg = imgage_record['jpeg'] img_id = imgage_record['id'] print('extract features', records_path, '\n') jpg_data = tf.placeholder(dtype=tf.string) img = image_processing.process_image(jpg_data, 'train' in image_records) features = image_embedding.inception_v3([img], False, 'train' in image_records) saver = tf.train.Saver( tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope="InceptionV3")) with tf.Session() as sess: saver.restore(sess, cnn_model) with tf.python_io.TFRecordWriter(records_path) as writer: i = 1 while True: try: img_id_a, img_jpeg_a = sess.run([img_id, img_jpeg]) except tf.errors.OutOfRangeError as e: print('\nProcessed\n') break for caption in captions[img_id_a]: caption_ids = [ vocabulary.word_to_id(word) for word in caption ] img_a, features_a = sess.run( [img, features], feed_dict={jpg_data: img_jpeg_a}) context = tf.train.Features( feature={ "image_id": _int64_feature(img_id_a), "features": _floats_feature(features_a.astype(np.float)[0]), }) feature_lists = tf.train.FeatureLists( feature_list={ "caption": _bytes_feature_list([w.encode() for w in caption]), "caption_ids": _int64_feature_list(caption_ids) }) sequence_example = tf.train.SequenceExample( context=context, feature_lists=feature_lists) writer.write(sequence_example.SerializeToString()) progress(i, len(captions)) i += 1 print('\n')
def process(i): return process_image(img=i, x_start=700, y_start=400, y_stop=656, scales=__db['parameters']['scales'], classifier=__db['model'], scaler=__db['scaler'], parameters=__db['parameters'], heatmap_threshold=5)
def transform_image(transform_name, img_name): transform = transforms.get(transform_name) if transform: img_content = get_file(img_name) resp_content = "" if img_content: f = cStringIO.StringIO(img_content) outf, encoding = process_image(f, transform) content_type = mimetypes.guess_type("a.%s" % encoding) return make_response((outf.read(), 200, {'Content-Type': content_type})) return "404", 404
def init_env_data(game_state): # 초기 상태를 움직이지 않는 상태로 두고 이미지를 80 * 80 * 4 형태로 preprocessing함 """ do_nothing == [1, 0]""" do_nothing = np.zeros(5) do_nothing[0] = 1 """ game_state.frame_step => 행동 input을 기반으로 게임에 변화를 줌 x_t == 이미지 데이터, r_0 == reward, terminal == 게임이 끝났는지 여부""" x_t, r_0, terminal = game_state.frame_step(do_nothing) images = process_image(x_t) s_t = stack_images(images) return s_t
def update_env_by_action(game_state, s_t, a_t): """ x_t1_colored = 게임 이미지(컬러), r_t = 행동에 따른 보상값, terminal = 끝난 여부 """ x_t1_colored, r_t, terminal = game_state.frame_step(a_t) image = process_image(x_t1_colored) image = np.reshape(image, (80, 80, 1)) s_t1 = np.append(image, s_t[:, :, :3], axis=2) return s_t1, r_t, terminal
def get_wordsearch_and_filename(img, row_col_len): """ Returns a row_col_len*row_col_len array of the identified values the filename of the created images Processes inputted image Finds values Creates a GIF of the original image and the detected values image Calls get_value_from_image() for each cell in image """ # Generate a unique filename filename = uuid.uuid4().hex[:3] fixed_perspective = image_processing.process_image(img) if fixed_perspective is None: return None, filename cv2.imwrite(wordsearch_dir + filename + '_unsolved.jpg', fixed_perspective) fixed_perspective_gray = cv2.cvtColor(fixed_perspective, cv2.COLOR_BGR2GRAY) # Crop each box and pass its contents to get_value_from_image() # Add results to the matrix array matrix = [] x = y = 0 size = fixed_perspective.shape[0] // row_col_len for i in range(1, row_col_len + 1): for j in range(1, row_col_len + 1): cell = fixed_perspective_gray[y + (size * (i - 1)): y + (size * i + 1), x + (size * (j - 1)): x + (size * j + 1)] cropped = cell[0: cell.shape[1] - size // 10, int(cell.shape[0] / 10): int(cell.shape[0] - (cell.shape[0] / 10))] value = image_processing.get_value_from_image(img=cropped, templates=cached_letters_templates, template_size=(18, 22), threshold=0.39) matrix.append(value) if value is not 0: cv2.putText(fixed_perspective, str(value), (x + (size * j + 1) - int(size / 1.5), y + (size * i + 1) - int(size / 3.5)), cv2.FONT_HERSHEY_SIMPLEX, fontScale, (255, 100, 0), thickness, 2) # Output the detected_digits to file cv2.imwrite(wordsearch_dir + filename + '_detected.jpg', fixed_perspective) # Create a GIF of the detected digits and the unsolved sudoku frames = [wordsearch_dir + filename + '_unsolved.jpg', wordsearch_dir + filename + '_detected.jpg'] image_sequence = mpy.ImageSequenceClip(frames, fps=2) image_sequence.write_gif(wordsearch_dir + filename + '.gif', fps=2) rows = [] for i in range(0, row_col_len * row_col_len, row_col_len): rows.append(matrix[i:i + row_col_len]) return rows, filename
def get_tictactoe_and_filename(img): """ Finds the tictactoes values and writes to image Returns a 3*3 array of the identified values filename of the created image in static/images/tictactoes/ """ # Generate a unique filename filename = uuid.uuid4().hex[:3] fixed_perspective = image_processing.process_image(img) if fixed_perspective is None: return None, filename cv2.imwrite('static/images/tictactoes/' + filename + '_original.jpg', fixed_perspective) fixed_perspective_gray = cv2.cvtColor(fixed_perspective, cv2.COLOR_BGR2GRAY) # Crop each box and pass its contents to get_value_from_image() # Add results to the matrix array matrix = [] x = y = 0 size = fixed_perspective.shape[0] // 3 for i in range(1, 4): for j in range(1, 4): cell = fixed_perspective_gray[y + (size * (i - 1)):y + (size * i + 1), x + (size * (j - 1)):x + (size * j + 1)] cropped = cell[0:cell.shape[1] - size // 10, int(cell.shape[0] / 10):int(cell.shape[0] - (cell.shape[0] / 10))] value = image_processing.get_value_from_image( img=cropped, templates=cached_tictactoe_templates, template_size=(38, 57), threshold=0.25) matrix.append(value) if value is not 0: cv2.putText(fixed_perspective, str(value), (x + (size * j + 1) - int(size / 3), y + (size * i + 1)), cv2.FONT_HERSHEY_SIMPLEX, fontScale, (255, 100, 0), thickness, 2) cv2.imwrite('static/images/tictactoes/' + filename + '_detected.jpg', fixed_perspective) rows = [] for i in range(0, 9, 3): rows.append(matrix[i:i + 3]) return rows, filename
def transform_image(transform_name, img_name): transform = transforms.get(transform_name) if transform: img_content = get_file(img_name) resp_content = "" if img_content: f = cStringIO.StringIO(img_content) outf, encoding = process_image(f, transform) content_type = mimetypes.guess_type("a.%s" % encoding) return make_response((outf.read(), 200, { 'Content-Type': content_type })) return "404", 404
def compute_nn_features_ids(ids, net, coco): V = [] processed_ids = [] for img_id in tqdm(ids): img = coco.loadImgs(img_id)[0] img_path = op.join(data_dir, data_type, img["file_name"]) x = process_image(img_path) if len(x) > 0: x = np.expand_dims(x, axis=0) v = compute_nn_features(x, net, layer=2)[0] V.append(v) processed_ids.append(img_id) V = np.array(V) return V, processed_ids
def process_image(self, encoded_image, thread_id=0): """Decodes and processes an image string. Args: encoded_image: A scalar string Tensor; the encoded image. thread_id: Preprocessing thread id used to select the ordering of color distortions. Returns: A float32 Tensor of shape [height, width, 3]; the processed image. """ return image_processing.process_image(encoded_image, is_training=self.is_training(), height=self.config.image_height, width=self.config.image_width, thread_id=thread_id, image_format=self.config.image_format)
def handle(self, images): scores = [] for image in images: img = process_image(image) kp, des = self.orb.detectAndCompute(img, None) matches = self.bf.knnMatch(self.des, des, k=2) good = [] for m, n in matches: if m.distance < 0.75*n.distance: good.append([m]) scores.append(len(good)) return scores
def handle(self, image_data_uri): classifier = self.repo.get() if classifier is None: digits = load_digits() classifier = ClassifierFactory.create_with_fit( digits.data, digits.target ) self.repo.update(classifier) x = process_image(image_data_uri) if x is None: return 0 prediction = classifier.predict(x)[0] return prediction
def load_center_samples(driving_log_file): """ Loads and processes center images from a driving log. param: driving_log_file: the file name of the driving log returns: the tuple of the sample images and the corresponding steering angles """ driving_log = pd.read_csv(driving_log_file, header=None) n_rows = len(driving_log) X = np.empty(shape=((n_rows, ) + get_processed_image_shape()), dtype='uint8') Y = np.empty(shape=(n_rows, ), dtype='float32') pbar_rows = tqdm(range(len(driving_log)), unit=' samples') for pbar_row, (idx, row) in zip(pbar_rows, driving_log.iterrows()): img = cv2.imread(re.search('IMG\/.+', row[0]).group()) img = process_image(img) X[idx] = img Y[idx] = row[3] return X, Y
def telemetry(sid, data): # The current steering angle of the car steering_angle = data["steering_angle"] # The current throttle of the car throttle = data["throttle"] # The current speed of the car speed = data["speed"] # The current image from the center camera of the car imgString = data["image"] image = Image.open(BytesIO(base64.b64decode(imgString))) image_array = np.asarray(image) transformed_image_array = process_image(image_array)[None, :, :, :] # This model currently assumes that the features of the model are just the images. Feel free to change this. steering_angle = float(model.predict(transformed_image_array, batch_size=1)) # The driving model currently just outputs a constant throttle. Feel free to edit this. throttle = 0.3 print(steering_angle, throttle) send_control(steering_angle, throttle)
def predict(image_path, model, topk=5, processor='gpu'): ''' Predict the class (or classes) of an image using a trained deep learning model. ''' # TODO: Implement the code to predict the class from an image file model.eval() if processor == 'gpu': device = torch.device("cuda" if torch.cuda.is_available() else "cpu") else: device = 'cpu' model.to(device) # image processing image = process_image(image_path) # Convert the np.array to a Pytorch tensor image = torch.from_numpy(image).type(torch.cuda.FloatTensor) # Add dimension for batch image = image.unsqueeze(0) with torch.no_grad(): output = model.forward(image) ps = torch.exp(output) top_p, top_idx = ps.topk(topk, dim=1) model.train() # Convert to list top_p = top_p.detach().type(torch.FloatTensor).numpy().tolist()[0] top_idx = top_idx.detach().type(torch.FloatTensor).numpy().tolist()[0] # invert the dictionary 'class_to_idx' to obtain the classes of the top_probabilities via top indices idx_to_class = {value: key for key, value in model.class_to_idx.items()} top_classes = [idx_to_class[idx] for idx in top_idx] return top_p, top_classes
def predict(model, image_path, top_k=5, cat_class=None, device='cpu'): model.to(device) model.eval() with torch.no_grad(): image = torch.from_numpy(imp.process_image(image_path)) output = model(image.type(torch.float32).to(device).unsqueeze_(0)) ps = torch.exp(output) prob = ps.topk(top_k)[0].cpu().numpy() idx = ps.topk(top_k)[1].cpu().numpy() class_name = [] for key, value in model.class_to_idx.items(): if value in idx[0]: class_name.append(key) if cat_class is not None: with open(cat_class, 'r') as f: cat_to_name = json.load(f) flower_name = cat_to_name[class_name[0]] return flower_name, prob[0][0]
def process_edges(color_image,n_edge): thres_image = process_image(color_image,do_threshold=True,thres_val=90) #thres_edges, thres_cnts, thres_lines = edge_find(thres_image,0,150,250,dilate=1) thres_edges, thres_cnts, thres_lines = edge_find(thres_image,10,30,250,dilate=1) scanned_lines = 0 distances = 0 #thres_edges = cv2.erode(thres_edges,kernel,1) #averaged_thres_lines = average_over_nearby_lines(thres_lines) averaged_thres_lines = average_over_nearby_lines(select_lines(thres_lines,n_edge)) lines_img = deepcopy(color_image) if averaged_thres_lines: for l in averaged_thres_lines: cv2.line(lines_img,(l.x1,l.y1),(l.x2,l.y2),(0,0,255),2,cv2.LINE_AA) if(len(averaged_thres_lines) > 1): scanned_lines,distances = distance_between_lines(averaged_thres_lines[0],averaged_thres_lines[1],vertical=True) if scanned_lines: for l in scanned_lines: cv2.line(lines_img,(l.x1,l.y1),(l.x2,l.y2),(255,0,0),2,cv2.LINE_AA) return thres_edges, lines_img, thres_image, averaged_thres_lines, scanned_lines, distances
def img_proccessor(state): last_time = time.time() #20/140 monitor = {'top': 20, 'left': 0, 'width': 640, 'height': 360} original_img = np.array(sct.grab(monitor)) resized_img = cv2.resize(original_img, (640, 360)) fps_calc = 1 / (time.time() - last_time) fps = ('fps: {0}'.format(int(fps_calc))) new_img = cv2.putText(process_image(resized_img), fps, (522, 40), fontFace=4, color=[18, 153, 255], thickness=1, fontScale=0.8) #Color = BGR cv2.imshow('Processed', new_img)
def input_picture(): img_str = request.get_json().get("image") location = request.get_json().get("location") img_data = base64.b64decode(img_str) img_name = str(uuid.uuid4()) # Create unambigous image file name file_name = img_name + '.jpeg' # Write base64 string to image file with open(file_name, 'wb') as file: file.write(img_data) # Upload image data to blob storage, process and save price to db with open(file_name, 'rb') as file: obj = InputHandler(env_vars) obj.upload_picture_to_blob(file, img_name) price, fuel_type = process_image(file) obj.upload_price(price, fuel_type, location) os.remove(file_name) # Remove previously created file return "Image succesfully uploaded to blob storage!"
def image_processing(self): """ image_processing function process the image according user's request """ for index in self.processed_file_index: time_be = time() current_img = self.upload_file[index] decode_img = decode_b64_image(current_img, self.upload_file_type[index]) self.image_size_original.append(decode_img.shape) out_img, actions, size = process_image(decode_img, self.processing_type, self.actions) time_af = time() self.metrics.append(time_af - time_be) self.image_size_processed.append(size) self.processed_file.append( encode_nparray_to_img(out_img, self.upload_file_type[index])) self.actions = actions value = datetime.datetime.now() - self.upload_time self.processing_time = value.total_seconds()