def readLetters(self): """ Character recognition """ self.letters = [] i = 0 letters = [] for begin, end in self.segments: lett = self.img[1 : self.line_height - 2, begin - 1 : end + 2] # add a bit of padding to help with # recognition if lett.shape[1] == 0: continue # self.img.drawRectangle(begin-1, 1, end - begin+2, 28) if lett.shape[0] > lett.shape[1]: lett = resize(lett, height=30) else: lett = resize(lett, width=30) if lett.shape[0] > 0 and lett.shape[1] > 0: lett = embiggen(lett, (30, 30)) # cv2.imwrite("tmp\\char %d %s.jpg" % (i, "r"), lett) letters.append(lett.reshape(-1)) i += 1 try: characters = labels.inverse_transform(logistic_model.predict(letters)) print characters for charac, segment in zip(characters, self.segments): self.letters.append((charac, segment[0], segment[1])) except ValueError: print (map(lambda x: x.shape, letters))
def get(self): self.logger.info("Request to resize IMG from url") name = "/tmp/rs-%s.png" % str(uuid.uuid4()) args = self._get_args() resize(open_remote_image(self.get_query_argument("url")), name, args) self.response_file(name)
def getComicMetadata(self, path): ca = ComicArchive(path, default_image_path=AppFolders.missingPath("page.png")) if ca.seemsToBeAComicArchive(): if ca.hasMetadata( MetaDataStyle.CIX ): style = MetaDataStyle.CIX elif ca.hasMetadata( MetaDataStyle.CBI ): style = MetaDataStyle.CBI elif ca.hasMetadata( MetaDataStyle.COMET ): style = MetaDataStyle.COMET elif ca.hasMetadata( MetaDataStyle.CBW ): style = MetaDataStyle.CBW else: logging.warning(u"Library: File Has No ComicMeta Data") if ca.hasMetadata( MetaDataStyle.CALIBRE ): style = MetaDataStyle.CALIBRE elif ca.hasMetadata( MetaDataStyle.EPUB ): style = MetaDataStyle.EPUB else: style = None if style is not None: md = ca.readMetadata(style) if md.isEmpty: md = ca.metadataFromFilename() else: # No metadata in comic. make some guesses from the filename md = ca.metadataFromFilename() # patch version 2 if (md.title is None or md.title == "") and md.issue is None and not md.series is None: md.title = md.series md.series = None md.fingerprint = ca.fingerprint() md.path = ca.path md.page_count = ca.page_count md.mod_ts = datetime.utcfromtimestamp(getmtime(ca.path)) md.filesize = os.path.getsize(md.path) md.hash = "" #thumbnail generation image_data = ca.getPage(0, AppFolders.missingPath("cover.png")) #now resize it thumb = StringIO.StringIO() try: utils.resize(image_data, (400, 400), thumb) md.thumbnail = thumb.getvalue() except: md.thumbnail = None return md return None
def post(self): self.logger.info("Request to resize IMG for request file") args = self._get_args() for item in self.request.files.values(): for file_info in item: name = "/tmp/rs-%s-%s.png" % (time.time(), file_info["filename"]) resize(open_image(file_info["body"]), name, args) self.response_file(name) return
def save_pos_samples(root_folder, folder_to, train=True, subset_size=None): """Se encarga de cargar todos los samples positivos""" lst_file_folder = 'Train' if train else 'Test' lst_pos_file = os.path.join(lst_file_folder, 'pos.lst') lst_annotations_file = os.path.join(lst_file_folder, 'annotations.lst') content_pos = open(os.path.join(root_folder, lst_pos_file)) # Abro el listado de imagenes positivas content_annotations = open(os.path.join(root_folder, lst_annotations_file)) # Abro el listado de imagenes positivas content_pos_lines = content_pos.readlines() content_annotations_lines = content_annotations.readlines() # Si fue especificado un tamaño de subset recorto la lista de lineas if subset_size: combined = list(zip(content_pos_lines, content_annotations_lines)) # Los pongo en orden aleatorio cuando genero subset random.shuffle(combined) content_pos_lines, content_annotations_lines = zip(*combined) # Recorto el numero de resultados content_pos_lines = content_pos_lines[0:subset_size] content_annotations_lines = content_annotations_lines[0:subset_size] for img_path, bounding_boxes_path in zip(content_pos_lines, content_annotations_lines): # Elimino el caracter de nueva linea img_path = img_path.rstrip('\n') bounding_boxes_path = bounding_boxes_path.rstrip('\n') img_original = skimage.io.imread(os.path.join(root_folder, img_path)) # Cargo la imagen # Obtengo los bounding boxes de personas a recortar bounding_boxes_list = get_inria_bounding_boxes(root_folder, bounding_boxes_path) for bounding_box in bounding_boxes_list: persona = get_inria_bounding_box_cropped(img_original, bounding_box) # Recorto a la persona persona = utils.resize(persona) # Re escalo la imagen img_filename = utils.get_filename(img_path) # Genero el nombre que tendra la imagen guardada utils.save_img(persona, folder_to, img_filename) # Guardo la imagen en la carpeta de positivos
def transform(img): # # [TITLE ONLY] # # standardize the size of imput image. extract ONLY the title of the poster. # h,w = img.shape[:2] # imgT = img[:h/5,:] # crop out the title # imgT = utils.resize(imgT, CFG.STD_SIZE) # h,w = imgT.shape[:2] # title = [(0,0), (w-1,0), (w-1,h-1), (0,h-1)] # coords of the title is that of the img itself # standardize the size of imput image imgT = utils.resize(img, CFG.STD_SIZE) imgT = pTr.addOcclusions(imgT) h,w = imgT.shape[:2] r = CFG.TITLE_RATIO title = [(0,0), (w-1,0), (w-1,int(h*r)), (0,int(h*r))] imgT, title = pTr.lightBlob(imgT, title) imgT, title = pTr.blur(imgT, title) imgT, title = pTr.scaleAndTranslate(imgT,title) imgT, title = pTr.perspective(imgT, title) imgT, title = pTr.rotate(imgT,title) titleArea = utils.boundingArea(title) tBox = utils.formatLabel(titleArea, imgT.shape[:2]) return (imgT, tBox)
def detect_edges(image): image = utils.resize(image, height = 500) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (5, 5), 0) edges = utils.auto_canny(gray) return edges
def upload_url(request): albumform = PickAlbumForm(request.user, request.POST) urlform = UploadFromURLForm(request.POST) if albumform.is_valid() and urlform.is_valid(): url = urlform.cleaned_data['url'] album = albumform.cleaned_data['album'] name = urlparse(url).path.split('/')[-1] tmp_img = NamedTemporaryFile(delete=True) tmp_img.write(urllib2.urlopen(url).read()) tmp_img.flush() max_order = Photo.objects.filter(album=album).aggregate(Max('order'))['order__max'] or 0 path = 'albums/%s/%s/' % (request.user.id, album.id) photo = Photo(user=request.user, album=album) photo.image.save(name, File(tmp_img)) photo.image.open() # get the resizing dimensions from the preferences #TODO this might move to utils in the future preferences = Preferences.get_or_create(request.user) resize_dimensions = preferences.get_default_img_size() img_data = resize(photo.image, upload_to=path, sizes_data=[resize_dimensions], overwrite=True) for data in img_data: photo.width=data[1] photo.height=data[2] photo.order = max_order + 1 photo.save() p_id = photo.id return HttpResponse(p_id, mimetype="text/plain") return render(request, 'albums/uploadify_url.html', {'urlform': urlform})
def uploadify(request): # Processing of each uploaded image albumform = PickAlbumForm(request.user, request.POST) # import pdb; pdb.set_trace() if albumform.is_valid(): album = albumform.cleaned_data['album'] max_order = Photo.objects.filter(album=album).aggregate(Max('order'))['order__max'] or 0 img = request.FILES['Filedata'] path = 'albums/%s/%s/' % (request.user.id, album.id) #/media/albums/<userid>/<albumid>/<img>.jpg # get the resizing dimensions from the preferences #TODO this might move to utils in the future preferences = Preferences.get_or_create(request.user) resize_dimensions = preferences.get_default_img_size() img_data = resize(img, upload_to=path, sizes_data=[resize_dimensions]) # if img_data is not None: for data in img_data: photo = Photo(user=request.user, album=album, width=data[1], height=data[2]) photo.image = data[0] photo.order = max_order + 1 photo.save() p_id = photo.id return HttpResponse('%s' % p_id, mimetype="text/plain") #return the photo id # else: # return HttpResponse(_('File extension not in valid extensions: "%(extensions)s".' % { # 'extensions': ",".join(settings.VALID_IMG_EXTENSIONS) # }) # ) else: return HttpResponse()
def getValidation(self, params): if self.validation_dict == None: images = [] labels = [] # Read files for sample_filepath in self.validation_list: sample_fullpath = self.corpus_dirpath + '/' + sample_filepath + '/' + sample_filepath # IMAGE sample_img = cv2.imread(sample_fullpath + '.png', False) # Grayscale is assumed! height = params['img_height'] sample_img = utils.resize(sample_img, height) images.append(utils.normalize(sample_img)) # GROUND TRUTH if self.semantic: sample_full_filepath = sample_fullpath + '.semantic' else: sample_full_filepath = sample_fullpath + '.agnostic' sample_gt_file = open(sample_full_filepath, 'r') sample_gt_plain = sample_gt_file.readline().rstrip().split( utils.word_separator()) sample_gt_file.close() labels.append([self.word2int[lab] for lab in sample_gt_plain]) # Transform to batch image_widths = [img.shape[1] for img in images] max_image_width = max(image_widths) batch_images = np.ones(shape=[ len(self.validation_list), params['img_height'], max_image_width, params['img_channels'] ], dtype=np.float32) * self.PAD_COLUMN for i, img in enumerate(images): batch_images[i, 0:img.shape[0], 0:img.shape[1], 0] = img # LENGTH width_reduction = 1 for i in range(params['conv_blocks']): width_reduction = width_reduction * params[ 'conv_pooling_size'][i][1] lengths = [batch_images.shape[2] / width_reduction ] * batch_images.shape[0] self.validation_dict = { 'inputs': batch_images, 'seq_lengths': np.asarray(lengths), 'targets': labels, } return self.validation_dict, len(self.validation_list)
def paste_pose(background): # (75, 225) (243, 444) img_path = random.choice(pose_list) image = Image.open(img_path) image = resize(image, 166, 219) background.paste(image, (75-35, 225-38))
def images_align(): image = nib.load('20121114_075953s982300a000.nii.gz') affine = image.affine data = np.array(image.dataobj[:, :, :, 2]) image_nib = nib.Nifti1Image(data, affine=affine) new_image_nib = resize(image_nib, (96, 96, 106), interpolation="nearest") nib.save(new_image_nib, 'test.nii') print(new_image_nib)
def pred_func(objects, last_output, gt_size): obj_pred = last_output[0].clone() for pred, o in zip(last_output[1:], objects): obj_pred[:, o] = pred[:, o] obj_pred = utils.resize(obj_pred, gt_size) obj_pred = obj_pred.argmax(dim=1) return obj_pred, None
def extract(path): img = cv2.imread(path, cv2.IMREAD_COLOR) img = resize(img, 300) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.1, 3) if len(faces) <= 0: return None bodies = body_cascade.detectMultiScale(gray, 1.1, 3) if len(bodies) <= 0: return None face = Rectangle(faces[0][0], faces[0][1], faces[0][2], faces[0][3]) body = Rectangle(bodies[0][0], bodies[0][1], bodies[0][2], bodies[0][3]) # Body/Face Section body_ratio = body.w / face.w if body_ratio <= BODY_RATIO_THRESHOLD: return None # Skin Section skinR = skinG = skinB = 0 for col in range(face.half_w): for row in range(10): pixel = img[face.y+face.half_h-row+5, face.x+face.quarter_w+col] skinB += pixel[0] skinG += pixel[1] skinR += pixel[2] averageDivisor = face.half_w * 10 skinR = int(skinR / averageDivisor) skinG = int(skinG / averageDivisor) skinB = int(skinB / averageDivisor) # Hair Section hairR = hairG = hairB = 0 top = face.y - 5 if top < 0: top = 0 for col in range(face.half_w): for row in range(10): pixel = img[top+row, face.x+face.quarter_w+col] hairB += pixel[0] hairG += pixel[1] hairR += pixel[2] hairR = int(hairR / averageDivisor) hairG = int(hairG / averageDivisor) hairB = int(hairB / averageDivisor) # Contour Section contour = recognize_contour(img[face.y:face.y2, face.x:face.x2]) print("{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}".format(os.path.basename(path), skinR, skinG, skinB, hairR, hairG, hairB, body_ratio, contour))
def get_next_batch(batch_size=128): images = [] to_images = [] max_width_image = 0 font_min_length = random.randint(10, 20) for i in range(batch_size): font_name = random.choice(AllFontNames) font_length = random.randint(font_min_length-5, font_min_length+5) font_size = random.randint(9, 64) font_mode = random.choice([0,1,2,4]) font_hint = random.choice([0,1,2,3,4,5]) text = utils_font.get_random_text(CHARS, eng_world_list, font_length) image = utils_font.get_font_image_from_url(text, font_name ,font_size, fontmode = font_mode, fonthint = font_hint ) image = utils_font.add_noise(image) image = utils_pil.convert_to_gray(image) rate = random.randint(8, 17) / font_size image = utils_pil.resize(image, rate) image = np.asarray(image) image = utils.resize(image, height=image_height) image = 255. - image images.append(image) to_image = utils_font.get_font_image_from_url(text, font_name ,image_height, fontmode = font_mode, fonthint = font_hint) to_image = utils_pil.convert_to_gray(to_image) to_image = np.asarray(to_image) to_image = utils.resize(to_image, height=image_height) to_image = utils.img2bwinv(to_image) to_images.append(to_image) if image.shape[1] > max_width_image: max_width_image = image.shape[1] if to_image.shape[1] > max_width_image: max_width_image = to_image.shape[1] max_width_image = max_width_image + (POOL_SIZE - max_width_image % POOL_SIZE) inputs = np.zeros([batch_size, max_width_image, image_height]) for i in range(len(images)): image_vec = utils.img2vec(images[i], height=image_height, width=max_width_image, flatten=False) inputs[i,:] = np.transpose(image_vec) labels = np.zeros([batch_size, max_width_image, image_height]) for i in range(len(to_images)): image_vec = utils.img2vec(to_images[i], height=image_height, width=max_width_image, flatten=False) labels[i,:] = np.transpose(image_vec) return inputs, labels
def resize_and_crop(phase, src, scale_size, crop_size): # resize the images src = resize(src, scale_size) # crop the images crop_params = get_crop_params(phase, src, crop_size) src = crop(src, crop_params[0], crop_params[1], crop_params[2], crop_params[3]) return src
def face_verify(): start = time.time() ensure_folder('static') file1 = request.files['file1'] fn_1 = secure_filename(file1.filename) full_path_1 = os.path.join('static', fn_1) file1.save(full_path_1) resize(full_path_1) file2 = request.files['file2'] fn_2 = secure_filename(file2.filename) full_path_2 = os.path.join('static', fn_2) file2.save(full_path_2) resize(full_path_2) prob, is_same = compare(full_path_1, full_path_2) elapsed = time.time() - start return is_same, prob, elapsed, fn_1, fn_2
def process_scans(self, image): image = segmentation_algo.get_slices_with_nodules(image) image = utils.resize(image) if self._augment: angle = randrange(-15, 15) image = utils.rotate_scans(image, angle) return utils.trim_pad_slices(image, pad_with_existing=True)
def after_resize(bg_size, card, coordinates): min_size=100 left, top, right, bottom = card.getbbox() bw, bh = bg_size if bw < right*bh/bottom: new_size = random.randint(min_size, bw) card = resize(card, width=new_size) else: new_size = random.randint(min_size, bh) card = resize(card, height=new_size) nw, nh = card.size coordinates = coordinates.astype('float') coordinates[:, 0] *= nw/right coordinates[:, 1] *= nh/bottom return card, coordinates
def copy_image(download_dir, overall_index, output_dir, args, i): img_path = get_img_path(i, download_dir) output_path = get_img_path(overall_index + i, output_dir) if args.resize_to is None: shutil.move(img_path, output_path) else: img = load_exr(img_path) resized = resize(img, args.resize_to) write_exr(output_path, resized)
def build_encoders(self, image): ycbcr = utils.RGB2YCbCr(image) encoders = [] M, N = image.shape[:2] for i in range(3): channel = ycbcr[:, :, i] if i == 0 else utils.resize( ycbcr[:, :, i], M // 2, N // 2) encoders.append(ZeroTreeEncoder(channel, WAVELET)) return encoders
def readImage(): img = cv2.imread('frame/ff206.png') img = resize(img,1500) im_blank = np.zeros_like(img) preProcess(img) imContours = img.copy() getContours(imThres) stack = imContours cv2.imshow("stack", stack) cv2.waitKey(20*1000)
def match_image(): start = time.time() ensure_folder(STATIC_DIR) ensure_folder(UPLOAD_DIR) file1 = request.files['file1'] fn_1 = secure_filename(file1.filename) full_path_1 = os.path.join(UPLOAD_DIR, fn_1) file1.save(full_path_1) resize(full_path_1) file2 = request.files['file2'] fn_2 = secure_filename(file2.filename) full_path_2 = os.path.join(UPLOAD_DIR, fn_2) file2.save(full_path_2) resize(full_path_2) is_match = compare(full_path_1, full_path_2) elapsed = time.time() - start return is_match, elapsed, fn_1, fn_2
def FEtimesampling(self, wf): #first time sampling: step = float(1./fesampling) tracelength = wf.length nrofpoints = int(tracelength/step) newtime = np.linspace(wf.tstart,wf.tend,nrofpoints) [a,b] = utils.resize(wf.time,wf.amp) newy = np.interp(newtime,a,b) newwf = waveform.Waveform(newtime,newy,'timesampled') return newwf
def scan(file): image = Image.open(file.stream) image = utils_pil.convert_to_gray(image) image = np.asarray(image) utils.save(image, os.path.join(curr_dir, "test", "p0.png")) # image = utils.clearImgGray(image) # utils.save(image * 255, os.path.join(curr_dir,"test","p1.png")) split_images = utils.splitImg(image) ocr_texts = [] for i, split_image in enumerate(split_images): inv_image = utils.img2bwinv(split_image) inv_image = utils.clearImg(inv_image) image = 255. - split_image image = utils.dropZeroEdges(inv_image, image) image = utils.resize(image, ocr.image_height) image = image / 255. ocr_inputs = np.zeros([1, ocr.image_size, ocr.image_size]) ocr_inputs[0, :] = utils.square_img( image, np.zeros([ocr.image_size, ocr.image_size])) utils.save(ocr_inputs[0] * 255, os.path.join(curr_dir, "test", "ocr_%s.png" % i)) ocr_seq_len = np.ones(1) * ocr.SEQ_LENGHT start = time.time() # p_net_g = session.run(net_g, {inputs: ocr_inputs}) # p_net_g = np.squeeze(p_net_g, axis=3) # debug_net_g = np.copy(p_net_g) # for j in range(1): # _t_img = utils.unsquare_img(p_net_g[j], ocr.image_height) # _t_img[_t_img<0] = 0 # _t_img = utils.cvTrimImage(_t_img) # _t_img = utils.resize(_t_img, ocr.image_height) # if _t_img.shape[0] * _t_img.shape[1] <= ocr.image_size * ocr.image_size: # p_net_g[j] = utils.square_img(_t_img, np.zeros([ocr.image_size, ocr.image_size]), ocr.image_height) # _img = np.vstack((ocr_inputs[0], debug_net_g[0], p_net_g[0])) # utils.save(_img * 255, os.path.join(curr_dir,"test","%s.png"%i)) decoded_list = session.run(res_decoded[0], { inputs: ocr_inputs, seq_len: ocr_seq_len }) seconds = round(time.time() - start, 2) print("filished ocr %s , paid %s seconds" % (i, seconds)) detected_list = utils.decode_sparse_tensor(decoded_list) for detect_number in detected_list: ocr_texts.append(ocr.list_to_chars(detect_number)) return ocr_texts
def load_stylegan_avatar(): url = "https://thispersondoesnotexist.com/image" r = requests.get(url, headers={'User-Agent': "Impersonator 0.1.0"}).content image = np.frombuffer(r, np.uint8) image = cv2.imdecode(image, cv2.IMREAD_COLOR) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = resize(image, (IMG_SIZE, IMG_SIZE)) return image
def evaluate(track): mix_audio, orig_sr, mix_channels = track.audio, track.rate, track.audio.shape[ 1] if mix_channels > 1: mono_audio = librosa.to_mono(mix_audio.T) else: mono_audio = mix_audio mono_audio = pad_audio(mono_audio, orig_sr) if orig_sr != hp.sample_rate: mono_audio = librosa.resample(mono_audio, orig_sr, hp.sample_rate) estimates = model.generate_wav(device, mono_audio) if hp.sample_rate != orig_sr: resample(estimates, orig_sr) resize(estimates, mix_audio) if mix_channels > 1: replicate_channels(estimates, mix_channels) #scores = museval.eval_mus_track( # track, estimates, output_dir='bss_evals') #print(scores) return estimates
def __call__(self, x_batch, y_batch): images, landmarks = [], [] for img_file, pts_file in zip(x_batch, y_batch): img = imread(img_file) xs, ys = read_landmarks(pts_file) pts = np.r_[xs, ys] cropped = crop(img, pts, padding=self.padding) new_img, new_pts = resize(*cropped, target_size=self.target_size) images.append(new_img) landmarks.append(new_pts) return np.array(images), np.array(landmarks)
def paste_image(background_image, text, kind, space_width=3, height=25): image, mask = _generate_horizontal_text(text, font_size=32, space_width=space_width) image = resize(image, height=height) idw = image.size[0] x = (positions[kind][1][0] + positions[kind][0][0] - idw) // 2 + random.randint(-10, 10) y = positions[kind][0][1] background_image.paste(image, (x, y), image) return image
def match_video(): start = time.time() ensure_folder(STATIC_DIR) ensure_folder(UPLOAD_DIR) file = request.files['file'] upload_file = secure_filename(file.filename) full_path = os.path.join(UPLOAD_DIR, upload_file) file.save(full_path) resize(full_path) print('full_path: ' + full_path) with torch.no_grad(): x = gen_feature(full_path) cosine = np.dot(features, x) cosine = np.clip(cosine, -1, 1) print('cosine.shape: ' + str(cosine.shape)) max_index = int(np.argmax(cosine)) max_value = cosine[max_index] name = name_list[max_index] fps = fps_list[max_index] idx = idx_list[max_index] image_fn = image_fn_list[max_index] print('max_index: ' + str(max_index)) print('max_value: ' + str(max_value)) print('name: ' + name) print('fps: ' + str(fps)) print('idx: ' + str(idx)) theta = math.acos(max_value) theta = theta * 180 / math.pi print('theta: ' + str(theta)) prob = get_prob(theta) print('prob: ' + str(prob)) time_in_video = idx / fps print('time_in_video: ' + str(time_in_video)) prob = get_prob(theta) elapsed = time.time() - start return name, prob, idx, float(time_in_video), float(elapsed), str( upload_file), image_fn
def __call__(self, pred, obj_gt, part_gt): obj_pred, part_pred = pred obj_gt = obj_gt.squeeze(dim=1) part_gt = part_gt.squeeze(dim=1) pred_size = obj_pred.shape[-2:] obj_gt = utils.resize(obj_gt, pred_size) part_gt = utils.resize(part_gt, pred_size) part_gt = self.tree.split_parts_gt(obj_gt, part_gt, mark_in_obj=False) obj_loss = self.obj_ce(obj_pred, obj_gt) part_loss = [] for o, (start, end) in self.tree.obj2part_idx.items(): obj_mask = obj_gt == o o_part_gt = part_gt[self.tree.obj2idx[o]].clone() o_part_gt[~obj_mask] = -1 o_part_pred = part_pred[:, start:end] part_loss.append(self.part_ce(o_part_pred, o_part_gt)) loss = obj_loss + sum(part_loss) return loss
def descriptors_from_class(dataset, class_img_paths, class_number, option=constants.ORB_FEAT_OPTION): """ Gets all the local descriptors for a class. If an image has a side with more than 640 pixels it will be resized leaving the biggest side at 640 pixels and conserving the aspect ratio for the other side. Args: dataset (Dataset object): An object that stores information about the dataset. class_img_paths (array of strings): The paths for each image in certain class. class_number (integer): The number of the class. option (integer): If this is 49 (The key '1') uses ORB features, else use SIFT. Returns: numpy float matrix: Each row are the descriptors found in an image of the class """ des = None #print class_img_paths step = (constants.STEP_PERCENTAGE * len(class_img_paths)) / 100 for i in range(len(class_img_paths)): img_path = class_img_paths[i] img = cv2.imread(img_path) resize_to = 640 h, w, channels = img.shape if h > resize_to or w > resize_to: img = utils.resize(img, resize_to, h, w) if option == constants.ORB_FEAT_OPTION: des_name = "ORB" new_des = orb(img) else: des_name = "SIFT" new_des = sift(img) if new_des is not None: if des is None: des = nmpy.array(new_des, dtype=nmpy.float32) #print "YES" else: des = nmpy.vstack((des, nmpy.array(new_des))) #print "NO" # Print a message to show the status of the function if i % step == 0: percentage = (100 * i) / len(class_img_paths) message = "Calculated {0} descriptors for image {1} of {2}({3}%) of class number {4} ...".format( des_name, i, len(class_img_paths), percentage, class_number) print(message) message = "* Finished getting the descriptors for the class number {0}*".format( class_number) print(message) print("Number of descriptors in class: {0}".format(len(des))) dataset.set_class_count(class_number, len(des)) return des
def load_mask(self, img, index): imgh, imgw = img.shape[0:2] mask_type = self.mask # external + random block if mask_type == 4: mask_type = 1 if np.random.binomial(1, 0.5) == 1 else 3 # external + random block + half elif mask_type == 5: mask_type = np.random.randint(1, 4) # random block if mask_type == 1: # return create_mask(imgw, imgh, imgw // 2, imgh // 2) return mask_generator.generate_random_mask( np.random.rand(np.random.randint(2, 5), 8), imgh, imgw) # half if mask_type == 2: # randomly choose right or left return create_mask(imgw, imgh, imgw // 2, imgh, 0 if random.random() < 0.5 else imgw // 2, 0) # external if mask_type == 3: mask_index = random.randint(0, len(self.mask_data) - 1) mask = imread(self.mask_data[mask_index]) mask = utils.resize(mask, imgh, imgw, self.is_center_crop) mask = (mask > 0).astype( np.uint8) * 255 # threshold due to interpolation return mask # test mode: load mask non random if mask_type == 6: mask = imread(self.mask_data[index % len(self.mask_data)]) mask = utils.resize(mask, imgh, imgw, centerCrop=False) mask = rgb2gray(mask) mask = (mask > 0).astype(np.uint8) * 255 return mask
def straighten(self, stepsize=3, low_angle=-5, high_angle=5): # width is shape[1], height is shape[0] #cv2.imwrite("tmp//before.jpg", self.img) origWidth = self.img.shape[1] if low_angle < -20 or high_angle > 20: raise Exception("Photo is too skewed. Please straighten photo before trying to process it") img = resize(self.img, width=600) # for some reason, straightening works better at this width :-?? # straighten out images # using histograms: rotate +-5 in .3 steps, get max of each histogram # and ideal rotation is argmax of those maxes simg = img hists = [] rng = list(range(low_angle*stepsize, high_angle*stepsize)) bincount = 600 if img.shape[0] > 600 else img.shape[0] for ang in rng: pimg = rotate(simg, ang/float(stepsize), fixed=True) # was true, but doesn't make sense and doens't work # pimg = rotate(simg, ang/float(stepsize)) # cv2.imwrite("tmp//rotate %d.jpg" % ang, pimg) hist, _ = np.histogram(pimg.sum(axis=1), bincount) #plt.plot(hist) #plt.savefig('tmp//hist %d.png' % ang, bbox_inches='tight') #plt.close() hists.append(max(hist)) rot = np.argmax(hists) # if the best rotation angle is the one on the edge of our threshold, try to rotate again with an extended # threshold in that direction if rot == 0: self.straighten(low_angle=low_angle-5, high_angle=high_angle-5) elif rot == len(rng) - 1: self.straighten(low_angle=low_angle+5, high_angle=high_angle+5) img = rotate(self.img, rng[rot]/float(stepsize), fixed=False) # otsu's method removes # background noise better # self.img = img.resize(w=origWidth//2) # so that all letters are small enough self.img = resize(img, width=600) # maybe I should look at average size of a blob ?
def left_blending(left_img, warped_img, disx, disy): points_left = approx(left_img) left_y, left_x = check_border(points_left) points_warped = approx(warped_img) warped_y, warped_x = check_border(points_warped) min_y_a = min(left_y) min_x_a = min(left_x) max_x_a = max(left_x) max_x = max(warped_x) max_y = max(warped_y) # 左图 A = left_img[0:max_x_a, min_y_a:disx, :].copy() A0 = left_img[disy + 200:disy + 300, disx:disx + 150, :] # 光照补偿块 # 右图 B = np.zeros((A.shape[0], max_y, 3), np.uint8) B[disy:disy + max_x, 0:max_y, :] = warped_img[0:max_x, 0:max_y, :] # 右图 B0 = B[disy + 200:disy + 300, 0:150, :] # 光照补偿参照块 k = xi(A0, B0) left_img = Recorver(left_img, k) #import sys #sys.exit() A_shape = A.shape[:2] B_shape = B.shape[:2] padding = 8 if padding % 2 != 0: padding = padding + 1 A1 = left_img[0:max_x_a, min_y_a:disx + 2 * padding, :].copy() std_size = resize(A_shape, B_shape, padding * 2) #print(std_size) # 标准大小 left_padding = img_padding(A1, std_size, padding, bi='left') right_padding = img_padding(B, std_size, padding, bi='right') #plt.imshow(left_padding) #plt.show() #plt.imshow(right_padding) #plt.show() result = multi_band_blending(left_padding, right_padding, padding * 2, 3, False) #print(left_padding.shape,right_padding.shape,result.shape) #plt.imshow(result) #plt.show() re = left_padding.shape[1] - A.shape[1] result1 = result[min_x_a:, re:, :].copy() return result1
def prepare_x(self, input_directory, do_resize=True): data_dir = pathlib.Path(input_directory) input_images_url = list(data_dir.glob('*')) if do_resize: train_x = np.asarray( [np.array(to_grayscale(resize(PIL.Image.open(str(image_url))))) for image_url in input_images_url]) print(train_x.shape) train_x = np.expand_dims(train_x, -1) else: train_x = [np.array(to_grayscale(PIL.Image.open(str(image_url)))) for image_url in input_images_url] print("#Images:", len(input_images_url)) print("train_x ready") return train_x
def load_item(self, index): size = self.input_size # load image ori_img = self.data[index] img = np.array(ori_img) # gray to rgb if len(ori_img.shape) < 3: ori_img = gray2rgb(ori_img) # resize/crop if needed if size != 0: img = utils.resize(ori_img, size, size, self.is_center_crop) # create grayscale image img_gray = rgb2gray(img) # load mask mask = self.load_mask(img, index) # resize/crop if needed if size != 0: img_shape = img.shape mask = utils.resize(mask, img_shape[0], img_shape[1], self.is_center_crop) # load edge edge = self.load_edge(img_gray, index, mask) # augment data if self.augment and np.random.binomial(1, 0.5) > 0: img = img[:, ::-1, ...] img_gray = img_gray[:, ::-1, ...] edge = edge[:, ::-1, ...] mask = mask[:, ::-1, ...] return list(ori_img.shape), self.to_tensor(img), self.to_tensor( img_gray), self.to_tensor(edge), self.to_tensor(mask)
def test(src_cam: cv.VideoCapture, dst_cam: cv.VideoCapture, affine_mat, size=(640, 480)): while True: # read images from both cameras is_ok, src_image = src_cam.read() if not is_ok: continue is_ok, dst_image = dst_cam.read() if not is_ok: continue # resize src_resized = utils.resize(src_image, size) dst_resized = utils.resize(dst_image, size) # transform src_resized = cv.warpAffine(src_resized, affine_mat, dst_resized.shape[::-1], flags=cv.INTER_LINEAR) # get BGR and Gray src_bgr, src_gray = utils.to_bgr_gray(src_resized) dst_bgr, dst_gray = utils.to_bgr_gray(dst_resized) # combine combine = (0.5 * src_bgr + 0.5 * dst_bgr).astype(np.uint8) # draw cv.imshow('combine', combine) key = cv.waitKey(10) if key == 27: break cv.destroyAllWindows()
def getComicMetadata(self, path): ca = ComicArchive( path, default_image_path=AppFolders.imagePath("default.jpg")) if ca.seemsToBeAComicArchive(): logging.debug(u"Reading in {0} {1}\r".format( self.read_count, path)) sys.stdout.flush() self.read_count += 1 if ca.hasMetadata(MetaDataStyle.CIX): style = MetaDataStyle.CIX elif ca.hasMetadata(MetaDataStyle.CBI): style = MetaDataStyle.CBI else: style = None if style is not None: md = ca.readMetadata(style) else: # No metadata in comic. make some guesses from the filename md = ca.metadataFromFilename() md.path = ca.path md.page_count = ca.page_count md.mod_ts = datetime.utcfromtimestamp(os.path.getmtime(ca.path)) md.filesize = os.path.getsize(md.path) md.hash = "" #thumbnail generation image_data = ca.getPage(0) #now resize it thumb = StringIO.StringIO() utils.resize(image_data, (200, 200), thumb) md.thumbnail = thumb.getvalue() return md return None
def get_shrunk_channels(self, src): shrink = self.options["shrink"] n_orient = self.options["n_orient"] grd_smooth_rad = self.options["grd_smooth_rad"] grd_norm_rad = self.options["grd_norm_rad"] luv = rgb2luv(src) size = (luv.shape[0] / shrink, luv.shape[1] / shrink) channels = [resize(luv, size)] for scale in [1.0, 0.5]: img = resize(luv, (luv.shape[0] * scale, luv.shape[1] * scale)) img = conv_tri(img, grd_smooth_rad) magnitude, orientation = gradient(img, grd_norm_rad) downscale = max(1, int(shrink * scale)) hist = histogram(magnitude, orientation, downscale, n_orient) channels.append(resize(magnitude, size)[:, :, None]) channels.append(resize(hist, size)) channels = N.concatenate(channels, axis=2) reg_smooth_rad = self.options["reg_smooth_rad"] / float(shrink) ss_smooth_rad = self.options["ss_smooth_rad"] / float(shrink) if reg_smooth_rad > 1.0: reg_ch = conv_tri(channels, int(round(reg_smooth_rad))) else: reg_ch = conv_tri(channels, reg_smooth_rad) if ss_smooth_rad > 1.0: ss_ch = conv_tri(channels, int(round(ss_smooth_rad))) else: ss_ch = conv_tri(channels, ss_smooth_rad) return reg_ch, ss_ch
def getComicMetadata(self, path): ca = ComicArchive(path, default_image_path=AppFolders.imagePath("default.jpg")) if ca.seemsToBeAComicArchive(): logging.debug(u"Reading in {0} {1}\r".format(self.read_count, path)) sys.stdout.flush() self.read_count += 1 if ca.hasMetadata( MetaDataStyle.CIX ): style = MetaDataStyle.CIX elif ca.hasMetadata( MetaDataStyle.CBI ): style = MetaDataStyle.CBI else: style = None if style is not None: md = ca.readMetadata(style) else: # No metadata in comic. make some guesses from the filename md = ca.metadataFromFilename() md.path = ca.path md.page_count = ca.page_count md.mod_ts = datetime.utcfromtimestamp(os.path.getmtime(ca.path)) md.filesize = os.path.getsize(md.path) md.hash = "" #thumbnail generation image_data = ca.getPage(0) #now resize it thumb = StringIO.StringIO() utils.resize(image_data, (200, 200), thumb) md.thumbnail = thumb.getvalue() return md return None
def upload(request): """ Pure HTTP based upload, if you don't want to or can't use Flash and/or javascript. """ amountform = AmountForm(request.GET) if amountform.is_valid(): amount = amountform.cleaned_data['amount'] or 20 else: amount = 20 PhotoFormSet = modelformset_factory(Photo, fields=('image',), extra=amount) if request.method == "POST": albumform = PickAlbumForm(request.user, request.POST) formset = PhotoFormSet(request.POST, request.FILES) if albumform.is_valid() and formset.is_valid(): album = albumform.cleaned_data['album'] photo_ids = [] max_order = Photo.objects.filter(album=album).aggregate(Max('order'))['order__max'] or 0 order = max_order + 1 path = 'albums/%s/%s/' % (request.user.id, album.id) # get the resizing dimensions from the preferences preferences = Preferences.get_or_create(request.user) resize_dimensions = preferences.get_default_img_size() i = 0 # to access the file in request.FILES for form in formset: if form.has_changed(): instance = form.save(commit=False) instance.user, instance.album = request.user, album instance.order = order img = request.FILES['form-%s-image' % i] img_data = resize(img, upload_to=path, sizes_data=[resize_dimensions]) for data in img_data: instance.width = data[1] instance.height = data[2] instance.image = data[0] instance.save() photo_ids.append(instance.id) order += 1 i += 1 #make it a GET request again to pass it to the next function request.method = "GET" return set_extra_info(request, photo_ids, album) else: albumform = PickAlbumForm(request.user) formset = PhotoFormSet(queryset=Photo.objects.none()) return render(request, 'albums/upload.html', {'amountform': amountform, 'albumform': albumform, 'formset': formset})
def descriptors_from_class(dataset, class_img_paths, class_number, option = constants.ORB_FEAT_OPTION): """ Gets all the local descriptors for a class. If an image has a side with more than 640 pixels it will be resized leaving the biggest side at 640 pixels and conserving the aspect ratio for the other side. Args: dataset (Dataset object): An object that stores information about the dataset. class_img_paths (array of strings): The paths for each image in certain class. class_number (integer): The number of the class. option (integer): If this is 49 (The key '1') uses ORB features, else use SIFT. Returns: numpy float matrix: Each row are the descriptors found in an image of the class """ des = None step = (constants.STEP_PERCENTAGE * len(class_img_paths)) / 100 for i in range(len(class_img_paths)): img_path = class_img_paths[i] img = cv2.imread(img_path) resize_to = 640 h, w, channels = img.shape if h > resize_to or w > resize_to: img = utils.resize(img, resize_to, h, w) if option == constants.ORB_FEAT_OPTION: des_name = "ORB" new_des = orb(img) else: des_name = "SIFT" new_des = sift(img) if new_des is not None: if des is None: des = np.array(new_des, dtype=np.float32) else: des = np.vstack((des, np.array(new_des))) # Print a message to show the status of the function if i % step == 0: percentage = (100 * i) / len(class_img_paths) message = "Calculated {0} descriptors for image {1} of {2}({3}%) of class number {4} ...".format( des_name, i, len(class_img_paths), percentage, class_number ) print(message) message = "* Finished getting the descriptors for the class number {0}*".format(class_number) print(message) print("Number of descriptors in class: {0}".format(len(des))) dataset.set_class_count(class_number, len(des)) return des
def update_PCW(self, uNodeState, uInputInfo): """Update the PCW matrix.""" PCW = uNodeState['PCW'] k = uNodeState['k'] cls = uInputInfo['class'] try: PCW[k, cls] += 1 except: (rows, cols) = PCW.shape (delta_r, delta_c) = (k + 1 - rows, cls + 1 - cols) if delta_r < 0: delta_r = 0 if delta_c < 0: delta_c = 0 PCW = utils.resize(PCW, (rows + delta_r, cols + delta_c)) PCW[k, cls] = 1 uNodeState['PCW'] = PCW
def analyze(self): """ Character segmentation """ prev = 0 i = size true_query = [] nimg = np.concatenate( (np.zeros((20, size)), resize(self.img, width=self.img.shape[1], height=20), np.zeros((20, size))), axis=1 ) all_black = True while i < self.img.shape[1]: mimg = nimg[:, i - size : i + size] mimg = mimg.reshape(-1).astype(float) # nimg = pipe.transform(nimg) probabilities = model.predict_proba(mimg)[0] # print(probabilities) if probabilities[0] > probabilities[1]: pred = 0 else: pred = 1 if all_black and nimg[:, i].sum() > 50: all_black = False # cv2.imwrite("tmp\\seg %d %s.jpg" % (i, pred), mimg.reshape((20,20))) if pred == 1: true_query.append((i - size, probabilities[1], all_black)) all_black = True if 5 < i - prev < 30: prev = i i += 5 else: prev = i i += 1 self.segments = [] prev = 0 for i, prob, all_black in true_query: if not all_black and nimg[:, prev + size : i + size].sum() > 200: self.segments.append((prev, i)) prev = i if nimg[:, prev + size :].sum() > 200: self.segments.append((prev, self.img.shape[1] - 1))
def prepareByFile(imgObject, detector, w, h): ''' resize, возвращение подготовленного для сравнения объекта(-ов) :param imgObject: изображение :param detector: путь до изображения :param w: ширина изображения после resize :param h: высота изображения после resize :returns kp: особые точки изображения :returns desc: описания особых точек изображения ''' img = utils.readFromFile(imgObject) img = utils.resize(img, w, h) print 'PREPARE ' if not detector: raise Exception("Detector can't be None") kp, desc = detector.detectAndCompute(img, None) return kp, desc
def prepare(imgPath, detector, w, h): ''' Чтение изображения, resize, возвращение подготовленного для сравнения объекта(-ов) :param imgPath: путь до изображения :param detector: путь до изображения :param w: ширина изображения после resize :param h: высота изображения после resize :returns kp: особые точки изображения :returns desc: описания особых точек изображения ''' img = utils.read(imgPath) print img if img == None: raise Exception(u"Can't open file '%s'" % imgPath) img = utils.resize(img, w, h) print u'PREPARE %s' % imgPath if not detector: raise Exception("Detector can't be None") kp, desc = detector.detectAndCompute(img, None) return kp, desc
import utils import constant import glob #temp, gain, bw, tau of power det datafolder = constant.calibdatafolder+ '/2013_03_25/calib_board/' files1 = glob.glob(datafolder+'C1pdboard200*') files2 = glob.glob(datafolder+'C2pdboard200*') mean1 = np.array([]) mean2 = np.array([]) vminusmean1 = np.array([]) vminusmean2 = np.array([]) for f1,f2 in zip(files1[::5],files2[::5]): wf1 = utils.readscopefile(f1) wf2 = utils.readscopefile(f2) [pd,board] = utils.resize(wf1[1],wf2[1]) delay = utils.finddelay2(pd,board) board = np.roll(board,delay) m1 = np.mean(pd) m2 = np.mean(board) #DC component mean1 = np.append(mean1,m1) mean2 = np.append(mean2,m2) #non DC component vminusmean1 = np.append(vminusmean1, pd - m1) vminusmean2 = np.append(vminusmean2, board - m2) fig1 = plt.figure() ax1 = plt.subplot(111) ax1.plot(mean1,mean2,'ro',label='mean waveform') fit = np.polyfit(mean1,mean2,1)
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml') body_cascade = cv2.CascadeClassifier('haarcascade_mcs_upperbody.xml') if __name__ == '__main__': arg = Arguments(sys.argv) fullpath = arg.get_as_file(0) # Check the file is exist if fullpath is None: sys.stderr.write(USAGE) exit(1) print('File:\t', fullpath) img = cv2.imread(fullpath, cv2.IMREAD_COLOR) img = resize(img, 300) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.1, 3) print('Face detect:\t', len(faces) > 0) bodies = body_cascade.detectMultiScale(gray, 1.1, 3) print('Body detect:\t', len(bodies) > 0) if len(faces) <= 0 or len(bodies) <= 0: exit(1) face = Rectangle(faces[0][0], faces[0][1], faces[0][2], faces[0][3]) body = Rectangle(bodies[0][0], bodies[0][1], bodies[0][2], bodies[0][3]) # Skin Section skinR = skinG = skinB = 0
#for f1, f2 in zip(filesRF, filesPD): for f1, f2 in zip(filesRF[::5], filesPD[::5]): mindist = 1e6 #result for each file dist = np.array([]) besttau = 0 besta = 0 bestb = 0 wfRF = utils.readscopefile(f1) wfPD = utils.readscopefile(f2) for t in taus: conv = utils.produceresponse(wfRF[0],wfRF[1],t) real = wfPD[1] sim = conv[1] #resize the two waveforms to the same size (because of the convolution) [real,sim] = utils.resize(real,sim) time = utils.gettime(wfPD[0],conv[0]) delay = utils.finddelay2(real,sim) simshifted = np.roll(sim,delay) #fit the conv vs power: fitconv_pd = np.polyfit(simshifted,real,1) polyconv_pd = np.poly1d(fitconv_pd) simpd = polyconv_pd(simshifted) size = len(simpd) alpha = np.sum( (simpd - real)**2) dist = np.append(dist,alpha) if alpha < mindist: mindist = alpha besttau = t besta = fitconv_pd[0] bestb = fitconv_pd[1]
X = [] y = [] resize_to = 640 #des = None print ('start loading ' + str(len(pos_img_files)-1) + ' positive files') for pos_img_file in pos_img_files: pos_filepath = pos_img_dir + pos_img_file # not include hidden files root, ext = os.path.splitext(pos_filepath) if ext == ".JPG" : print(pos_filepath) pos_img = cv2.imread(pos_filepath) h, w, channels = pos_img.shape if h > resize_to or w > resize_to: pos_img = utils.resize(pos_img, resize_to, h, w) gray_pos= cv2.cvtColor(pos_img,cv2.COLOR_BGR2GRAY) #orb orb = cv2.ORB_create() kp_orb, des = orb.detectAndCompute(gray_pos, None) img_orb = cv2.drawKeypoints(gray_pos, kp_orb, None) #akaze #akaze = cv2.AKAZE_create() #kp_akaze, des = akaze.detectAndCompute(gray_pos, None) #img_akaze = cv2.drawKeypoints(gray_pos, kp_akaze, None) #fast #detect keypoints #fast = cv2.FastFeatureDetector_create()
def __init__(self, begin, end, img): self.img = resize(img, height=Line.line_height) self.beginY = begin self.endY = end