def demo(model, img, with_gpu): im_resized, (ratio_h, ratio_w) = resize_image(img) im_resized = im_resized.astype(np.float32) im_resized = torch.from_numpy(im_resized) if with_gpu: im_resized = im_resized.cuda() im_resized = im_resized.unsqueeze(0) im_resized = im_resized.permute(0, 3, 1, 2) score, geometry = model.forward(im_resized) score = score.permute(0, 2, 3, 1) geometry = geometry.permute(0, 2, 3, 1) score = score.detach().cpu().numpy() geometry = geometry.detach().cpu().numpy() boxes = detect(score_map=score, geo_map=geometry) if len(boxes) > 0: boxes = boxes[:, :8].reshape((-1, 4, 2)) boxes[:, :, 0] /= ratio_w boxes[:, :, 1] /= ratio_h if boxes is not None: for box in boxes: box = sort_poly(box.astype(np.int32)) img = cv2.polylines(img, [box.astype(np.int32).reshape((-1, 1, 2))], True, color=(0, 255, 0), thickness=1) return img
def _load_dataset(self): file_path = os.path.join(self._root, self._opt.train_list) with codecs.open(file_path, 'r', 'utf-8') as fr: lines = fr.readlines() for ind, line in enumerate(lines): context = line.strip().split() image_path = os.path.join(self._root, context[0]) label = int(context[1]) img = cv2.imread(image_path) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = resize_image(img, self._img_size, self._img_size) img = np.asarray(img, dtype='float32') self.datas.append([img, label])
def test(self): img = cv2.imread(self._img_path) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = resize_image(img, self._img_width, self._img_height) img = np.asarray(img, dtype='float32') img = np.transpose(img, [2, 0, 1]) img = img.reshape((1, ) + img.shape) #(1,3,1200,1600) vertices, rects = image_proposal(self._img_path) #rects: x,y,w,h show_rect(self._img_path, rects, ' ') verts = [] for item in vertices: item = item[:4] for i in range(len(item)): item[i] = item[i] / 16.0 verts.append([0.] + item) cls, bbox = self._model._forward_test(img, verts) #cls = F.softmax(cls,dim = 1) cls = torch.argmax(cls, dim=1).data.cpu().numpy() bbox = bbox.data.cpu().numpy() tmp = sorted(zip(rects, bbox, cls), key=lambda x: x[1][0], reverse=True) rects, bbox, cls = zip(*tmp) #print (self._class_to_ind) index = 0 cls = self._class_to_ind[cls[0]] show_rect(self._img_path, [rects[index]], cls) px, py, pw, ph = rects[index] old_center_x, old_center_y = px + pw / 2.0, py + ph / 2.0 x_ping, y_ping, w_suo, h_suo = bbox[index][1:] new_center_x = x_ping * pw + old_center_x new_center_y = y_ping * ph + old_center_y new_w = pw * np.exp(w_suo) new_h = ph * np.exp(h_suo) new_verts = [new_center_x, new_center_y, new_w, new_h] show_rect(self._img_path, [new_verts], cls)
def demo(model, img_path, save_path, with_gpu): with torch.no_grad(): im = cv2.imread(img_path)[:, :, ::-1] im_resized, (ratio_h, ratio_w) = resize_image(im, 512) im_resized = im_resized.astype(np.float32) im_resized = torch.from_numpy(im_resized) if with_gpu: im_resized = im_resized.cuda() im_resized = im_resized.unsqueeze(0) im_resized = im_resized.permute(0, 3, 1, 2) score, geometry = model.forward(im_resized) score = score.permute(0, 2, 3, 1) geometry = geometry.permute(0, 2, 3, 1) score = score.detach().cpu().numpy() geometry = geometry.detach().cpu().numpy() boxes = detect(score_map=score, geo_map=geometry) if len(boxes) > 0: boxes = boxes[:, :8].reshape((-1, 4, 2)) boxes[:, :, 0] /= ratio_w boxes[:, :, 1] /= ratio_h if boxes is not None: for box in boxes: box = sort_poly(box.astype(np.int32)) if np.linalg.norm(box[0] - box[1]) < 5 or np.linalg.norm(box[3] - box[0]) < 5: continue cv2.polylines(im[:, :, ::-1], [box.astype(np.int32).reshape((-1, 1, 2))], True, color=(0, 255, 0), thickness=2) cv2.imwrite( os.path.join(save_path, 'result-{}'.format(img_path.split('/')[-1])), im[:, :, ::-1])
def myphotos(): """login required my photos route""" all_labels = ["Labels generating asynchronously"] prefix = "photos/" ##### # Getting list of photos from database ##### photos = database.list_photos(flask_login.current_user.id) for photo in photos: photo["signed_url"] = s3Util.generate_presigned_urls( config.PHOTOS_BUCKET, photo["object_key"]) form = PhotoForm() url = None if form.validate_on_submit(): image_bytes = util.resize_image(form.photo.data, (300, 300)) if image_bytes: ####### # s3 excercise - save the file to a bucket ####### key = prefix + util.random_hex_bytes(8) + '.png' s3Util.put_object(config.PHOTOS_BUCKET, key, image_bytes, "image/png") # create labels from amazon rekoginition # Code moved to Lambda function #all_labels = rekognitionUtil.detect_labels(config.PHOTOS_BUCKET, key) # Generate pre signed url for newly uploaded photo url = s3Util.generate_presigned_urls(config.PHOTOS_BUCKET, key) # save the image labels to database database.add_photo(key, ", ".join(all_labels), form.description.data, flask_login.current_user.id) return render_template("index.html", form=form, url=url, photos=photos, all_labels=all_labels)
def predict(self, path, is_output_polygon=False, short_size=1024, output_folder='../output'): img = cv2.imread(path, 1 if self.img_mode != 'GRAY' else 0) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) h, w = img.shape[:2] img = resize_image(img, short_size) tensor = self.transform(img) tensor = tensor.unsqueeze_(0) tensor = tensor.to(self.device) batch = {'shape': [(h, w)]} with torch.no_grad(): if str(self.device).__contains__('cuda'): torch.cuda.synchronize(self.device) preds = self.model(tensor) if str(self.device).__contains__('cuda'): torch.cuda.synchronize(self.device) box_list, _ = self.post_process(batch, preds, is_output_polygon=is_output_polygon) box_list = box_list[0] if len(box_list) > 0: if is_output_polygon: idx = [x.sum() > 0 for x in box_list] box_list = [box_list[i] for i, v in enumerate(idx) if v] else: idx = box_list.reshape(box_list.shape[0], -1).sum(axis=1) > 0 box_list = box_list[idx] else: box_list, score_list = [], [] img = draw_bbox(cv2.imread(path)[:, :, ::-1], box_list) os.makedirs(output_folder, exist_ok=True) img_path = pathlib.Path(path) output_path = os.path.join(output_folder, img_path.stem + '_result.jpg') cv2.imwrite(output_path, img[:, :, ::-1]) return output_path, box_list
def get_batch(self): images = np.zeros( (self._batch_size, 3, self._img_height, self._img_width)) labels = np.zeros((self._batch_size, 1)) #R=128 n=2(batch_size) 128/2=64 Rois Rois = [] Verts = [] Roi_labels = [] count = 0 while (count < self._batch_size): image_idx = self.imgs[self.cursor] img_path = os.path.join(self._root, self._img_dir, image_idx) img = cv2.imread(img_path) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = resize_image(img, self._img_width, self._img_height) img = np.asarray(img, dtype='float32') img = np.transpose(img, [2, 0, 1]) #print (img.shape) images[count] = img labels[count] = self.labels[image_idx] data = self.datas[image_idx] data = sorted(data, key=lambda x: x[1].tolist(), reverse=True) data = data[:64] boxes = [] roi_labels = [] verts = [] for item in data: tmp = item[1].tolist() Roi_labels.append(tmp[0]) if tmp[0] > 0: tmp[0] = 1 boxes.append(tmp) vert = item[0][:4] for i in range(len(vert)): vert[i] = (vert[i] / 16.0) vert = [count] + vert verts.append(vert) #boxes = [item[1].tolist() for item in data] Rois += boxes Roi_labels += roi_labels #verts = [[count]+item[0][:4]/16.0 for item in data] #[ind_in_batch, x1,y1,x2,y2] Verts += verts count += 1 self.cursor += 1 if self.cursor >= len(self.imgs): self.cursor = 0 np.random.shuffle(self.imgs) #print(img_path, vert) Rois = np.array(Rois) Verts = np.array(Verts) Roi_labels = np.array(Roi_labels) return images, labels, Rois, Roi_labels, Verts