def show_emotions(img, faces_coords, predicts): img_detected = img.copy() font_scale = max(img.shape[1] // 600, 1) square_color = (50, 205, 50) text_color = (255, 255, 255) for coords, predict in zip(faces_coords, predicts): emocao = utils.get_emotion(np.argmax(predict)) (x, y, w, h) = coords font = cv2.FONT_HERSHEY_SIMPLEX x_pos = x + 1 * font_scale y_pos = y - 3 * font_scale font_size = 0.5 * font_scale font_thick = int(1 * np.ceil(font_scale / 2)) cv2.rectangle(img_detected, (x, y), (x + w, y + h), square_color, font_thick) cv2.rectangle(img_detected, (x, y - (16 * font_scale)), (x + w, y), square_color, -1) cv2.putText(img_detected, emocao, (x_pos, y_pos), font, font_size, text_color, font_thick, cv2.LINE_AA) utils.show_img(img_detected, rgb=True)
def select_ROI(img): r = cv2.selectROI(img) print(r) # Crop image imCrop = img[int(r[1]):int(r[1] + r[3]), int(r[0]):int(r[0] + r[2])] show_img(imCrop) print(imCrop.shape) return imCrop
def eval_patch(self): print("Eval with ", self.ocr_name) self.prep_model.eval() ori_lbl_crt_count = 0 ori_lbl_cer = 0 prd_lbl_crt_count = 0 prd_lbl_cer = 0 lbl_count = 0 counter = 0 for image, labels_dict in self.dataset: text_crops, labels = get_text_stack( image.detach(), labels_dict, self.input_size) lbl_count += len(labels) ocr_labels = self.ocr.get_labels(text_crops) ori_crt_count, ori_cer = compare_labels( ocr_labels, labels) ori_lbl_crt_count += ori_crt_count ori_lbl_cer += ori_cer image = image.unsqueeze(0) X_var = image.to(self.device) pred = self.prep_model(X_var) pred = pred.detach().cpu()[0] pred_crops, labels = get_text_stack( pred, labels_dict, self.input_size) pred_labels = self.ocr.get_labels(pred_crops) prd_crt_count, prd_cer = compare_labels( pred_labels, labels) prd_lbl_crt_count += prd_crt_count prd_lbl_cer += prd_cer ori_cer = round(ori_cer/len(labels), 2) prd_cer = round(prd_cer/len(labels), 2) if self.show_img: show_img(image.cpu()) if self.show_txt: self._print_labels(labels, pred_labels, ocr_labels) counter += 1 print() print('Correct count from predicted images: {:d}/{:d} ({:.5f})'.format( prd_lbl_crt_count, lbl_count, prd_lbl_crt_count/lbl_count)) print('Correct count from original images: {:d}/{:d} ({:.5f})'.format( ori_lbl_crt_count, lbl_count, ori_lbl_crt_count/lbl_count)) print('Average CER from original images: ({:.5f})'.format( ori_lbl_cer/lbl_count)) print('Average CER from predicted images: ({:.5f})'.format( prd_lbl_cer/lbl_count))
def faceDetection(image, classifier, hog, filename): """Scans for faces in a given image. Complete this function following the instructions in the problem set document. Use this function to also save the output image. Args: image (numpy.array): Input image. filename (str): Output image file name. Returns: None. """ height, width = image.shape[:2] stride = 32 x = 0 y = 0 img = np.copy(image) # gray_img= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) test_images = [] x_rects = [] y_rects = [] while y + stride < height: test_img = img[y:y + stride, x:x + stride] test_img = hog.compute(test_img).flatten() test_images.append(test_img) # print(prediction) # if prediction[0] == 1: # x_rects.append(x) # y_rects.append(y) x += 2 if x + stride >= width: x = 0 y += 2 t = np.array(test_images) print(t.shape) prediction = classifier.predict(t) print(prediction) xmean = int(np.mean(x_rects)) ymean = int(np.mean(y_rects)) cv2.rectangle(img, (xmean, ymean), (xmean + stride, ymean + stride), (0, 255, 50), 2) show_img(img) cv2.imwrite("output/{}.png".format(filename), img)
def find_radial_symmetry(img): # find regions of interest firstImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) firstImg = frst(firstImg, 200, 1, 0.5, 0.1, 'BRIGHT') # show_img(firstImg) firstImg = cv2.normalize(firstImg, None, 0.0, 1.0, cv2.NORM_MINMAX) # frstImage.convertTo(frstImage, CV_8U, 255.0); firstImg = cv2.convertScaleAbs(firstImg, None, 255.0, 0) show_img(firstImg) ret, thresh = cv2.threshold(firstImg, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) # show_img(ret) # cv::morphologyEx(inputImage, inputImage, operation, element, cv::Point(-1, -1), iterations); # bwMorph(frstImage, markers, cv::MORPH_CLOSE, cv::MORPH_ELLIPSE, 5); kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)) markers = cv2.morphologyEx(firstImg, cv2.MORPH_CLOSE, kernel, iterations=5) print(len(markers)) im2, contours, hierarchy = cv2.findContours(markers, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # print(len(contours)) # # img = cv2.drawContours(img, contours, -1, (0,0,255), 8) # show_img(img) moments = [] for cnt in contours: moment = cv2.moments(cnt) moments.append(moment) print(moment) #get the mass centers mass_centers = [] for moment in moments: x, y = moment.get('m10') / moment.get('m00'), moment.get( 'm01') / moment.get('m00') mass_centers.append((int(x), int(y))) for center in mass_centers: print(center) cv2.circle(img, center, 20, (0, 255, 0), 2) show_img(img) return None
def show_first_last_frames( self, frame_dir: Union[str, Path], fig_size: Tuple[float, float] = (20.0, 10.0) ) -> None: frame_dir = Path(frame_dir) # load first and last frames frame_path = frame_dir / f'{self.get_first_frame()}.jpg' first_frame = cv2.imread(str(frame_path), cv2.IMREAD_COLOR) frame_path = frame_dir / f'{self.get_last_frame()}.jpg' last_frame = cv2.imread(str(frame_path), cv2.IMREAD_COLOR) # concatenate frames first_last_frames = np.concatenate((first_frame, last_frame), axis=1) # plot image show_img(first_last_frames, fig_size=fig_size)
def main(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('--imgdir', type=str, default=None, help='directory to source images.') parser.add_argument('--outdir', type=str, default=None, help='directory to save output data.') parser.add_argument('--sp', type=int, default=50, help='spatial window radius for meanshift.') parser.add_argument('--sr', type=int, default=50, help='color window radius for meanshift.') parser.add_argument("--show_img", help='show image results', action="store_true") args = parser.parse_args() exp_name = 'sp{}_sr{}'.format(args.sp, args.sr) mkdir(args.outdir) files = os.listdir(args.imgdir) for filename in files: if filename.endswith('jpg'): full_path = os.path.join(args.imgdir, filename) # apply meanshift on image res = apply_meanshift(full_path, args.sp, args.sr) cv2.imwrite( os.path.join(args.outdir, exp_name + '-' + filename[:-4] + '_ms.jpg'), res) if args.show_img: window_name = 'Meanshift Result for {}'.format(filename) show_img(window_name, res)
def eval_area(self): print("Eval with ", self.ocr_name) self.prep_model.eval() pred_correct_count = 0 ori_correct_count = 0 ori_cer = 0 pred_cer = 0 counter = 0 for images, labels, names in self.loader_eval: X_var = images.to(self.device) img_preds = self.prep_model(X_var) ocr_lbl_pred = self.ocr.get_labels(img_preds.cpu()) ocr_lbl_ori = self.ocr.get_labels(images.cpu()) if self.show_txt: self._print_labels(labels, ocr_lbl_pred, ocr_lbl_ori) prd_crt_count, prd_cer = compare_labels( ocr_lbl_pred, labels) ori_crt_count, o_cer = compare_labels(ocr_lbl_ori, labels) pred_correct_count += prd_crt_count ori_correct_count += ori_crt_count ori_cer += o_cer pred_cer += prd_cer if self.show_img: show_img(img_preds.detach().cpu(), "Processed images") counter += 1 print() print('Correct count from predicted images: {:d}/{:d} ({:.5f})'.format( pred_correct_count, len(self.dataset), pred_correct_count/len(self.dataset))) print('Correct count from original images: {:d}/{:d} ({:.5f})'.format( ori_correct_count, len(self.dataset), ori_correct_count/len(self.dataset))) print('Average CER from original images: {:.5f}'.format( ori_cer/len(self.dataset))) print('Average CER from predicted images: {:.5f}'.format( pred_cer/len(self.dataset)))
def main(img_path, side_num): img = read_img(img_path) opened = pre_process(img) # find poly or circle if side_num == 0: approx_contours = find_circle(img, opened) else: approx_contours = find_poly(img, opened, side_num=side_num) # temp # find momentums momentum_img = get_blank_img(img.shape) cv2.drawContours(momentum_img, approx_contours, -1, (0, 0, 0), 2) for contour in approx_contours: center_x, center_y = get_momentum(contour) print("x,y: {},{}".format(center_x, center_y)) cv2.circle(momentum_img, (center_x, center_y), 7, 128, -1) # 绘制中心点 # show result show_img(momentum_img, 'momentums') cv2.imwrite("./image/momentum.png", momentum_img) cv2.waitKey(0) cv2.destroyAllWindows()
def find_signs(self, in_img): img = np.copy(in_img) # gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray_img = denoising(img) whirldata = self.detector.detectMultiScale(gray_img, 20, 20) return whirldata # add this # print(whirldata) candidate_signs = [] for (x, y, w, h) in whirldata: just_box = in_img[x:x + w, y:y + h, :] box_h, box_w, _ = just_box.shape if box_h == 0 or box_w == 0: continue # print('box shape', just_box.shape) # print(x,x+w, y, y+h) show_img(just_box) candidate_signs.append(just_box) # candidate_signs.append(just_box) # candidate_signs.append(just_box) # candidate_signs.append(just_box) # candidate_signs.append(just_box) font = cv2.FONT_HERSHEY_SIMPLEX cv2.rectangle(img, (x, y), (x + w, y + h), (255, 255, 0), 2) # cv2.putText(img, 'Sign',(x-w,y-h), font, 0.5, (11,255,255), 2, cv2.LINE_AA) show_img(img) # print(type(candidate_signs)) print(len(candidate_signs)) # # for region in candidate_signs: # # print(region) # print('region shape in', region.shape) return candidate_signs
def find_circles(img): # how well does it track the counters? img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img = cv2.medianBlur(img, 51) cimg = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) # circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,20, # param1=50,param2=30,minRadius=0,maxRadius=10) circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 20, param1=30, param2=15, minRadius=0, maxRadius=50) circles = np.uint16(np.around(circles)) for i in circles[0, :]: # draw the outer circle cv2.circle(cimg, (i[0], i[1]), i[2], (0, 255, 0), 2) # draw the center of the circle cv2.circle(cimg, (i[0], i[1]), 2, (0, 0, 255), 3) show_img(cimg)
def show_images(data_folder, pair): image_features = h5py.File(os.path.join(data_folder, IMAGES_FILENAME), "r") with open(os.path.join(data_folder, IMAGES_META_FILENAME), "r") as json_file: images_meta = json.load(json_file) word_map_file = os.path.join(data_folder, WORD_MAP_FILENAME) with open(word_map_file, "r") as json_file: word_map = json.load(json_file) _, _, test_images_split = get_splits_from_occurrences_data([pair]) for coco_id in test_images_split: image_data = image_features[coco_id][()] print("COCO ID: ", coco_id) for caption in images_meta[coco_id][DATA_CAPTIONS]: print(" ".join( decode_caption( get_caption_without_special_tokens(caption, word_map), word_map))) show_img(image_data) print("")
def train(self, cfg): _com_cfg = cfg.COM _train_cfg = cfg.TRAIN gen_img_path = os.path.join(_com_cfg.checkpoint, "gen_img") if not os.path.exists(gen_img_path): os.makedirs(gen_img_path, exist_ok=True) dis_net = Discriminator() gen_net = Generator(_com_cfg.z_dimension) if _train_cfg.use_cuda: dis_net = dis_net.cuda() gen_net = gen_net.cuda() train_loader = get_data_loader(cfg, "train") criterion = nn.BCELoss() dis_optim = torch.optim.Adam(dis_net.parameters(), lr=_train_cfg.lr) gen_optim = torch.optim.Adam(gen_net.parameters(), lr=_train_cfg.lr) gen_epoches = 3 for epoch_idx in range(_train_cfg.epochs): for iter_idx, (imgs, labels) in enumerate(train_loader): real_imgs = imgs real_labels = torch.ones(_train_cfg.batch_size) fake_labels = torch.zeros(_train_cfg.batch_size) if _train_cfg.use_cuda: real_imgs = real_imgs.cuda() real_labels = real_labels.cuda() fake_labels = fake_labels.cuda() # compute loss of real images real_out = dis_net(real_imgs) # print("real_out.size():{}".format(real_out.size())) # print("real_labels.size():{}".format(real_labels.size())) dis_loss_real = criterion(real_out, real_labels) real_scores = real_out # compute loss of fake images z_data = torch.randn(_train_cfg.batch_size, _com_cfg.z_dimension) if _train_cfg.use_cuda: z_data = z_data.cuda() fake_images = gen_net(z_data) fake_out = dis_net(fake_images) dis_loss_fake = criterion(fake_out, fake_labels) fake_scores = fake_out dis_loss = dis_loss_real + dis_loss_fake dis_optim.zero_grad() dis_loss.backward() dis_optim.step() for j in range(gen_epoches): fake_labels = torch.ones(_train_cfg.batch_size) z_data = torch.randn(_train_cfg.batch_size, _com_cfg.z_dimension) if _train_cfg.use_cuda: fake_labels = fake_labels.cuda() z_data = z_data.cuda() fake_img = gen_net(z_data) output = dis_net(fake_img) g_loss = criterion(output, fake_labels) gen_optim.zero_grad() g_loss.backward() gen_optim.step() if iter_idx % cfg.TRAIN.log_interval == 0: print('Epoch: {} [{}/{}], d_loss: {:.6f}, g_loss: {:.6f} D real: {:.6f}, D fake: {:.6f}'.format( epoch_idx, iter_idx, len(train_loader), dis_loss.item(), g_loss.item(), real_scores.data.mean(), fake_scores.data.mean())) utils.show_img(fake_img, epoch_idx, gen_img_path)
import numpy as np import tensorflow as tf import os import cv2 from model import LPModel import dataset from utils import draw_bbox, show_img Weights = '.\\checkpoint\\cp6.ckpt' sample_data = '.\\data\\br\\JRK5336.jpg' if __name__ == '__main__': model = LPModel() model.load_weights(Weights) origin_img = cv2.imread(sample_data) img_h, img_w, c = origin_img.shape input_img = np.expand_dims(dataset.transform_img(origin_img), 0) out = model.model.predict(input_img)[0] x, y, w, h = out print(out) x1, x2 = int((x - w / 2) * 416), int((x + w / 2) * 416) y1, y2 = int((y - h / 2) * 416), int((y + h / 2) * 416) img = draw_bbox(input_img[0], (x1, y1, x2, y2)) show_img(img)
def show_images(data_folder, image_ids): # Read image and process h5py_file = h5py.File(os.path.join(data_folder, IMAGES_FILENAME), "r") for image_id in image_ids: image_data = h5py_file[image_id].value show_img(image_data)
def show_strip(): #img = cv2.imread('input/cleaned/pos_stop/7839_stop_1324866481.avi_image26.png') img = cv2.imread( 'input/lisa/vid0/frameAnnotations-vid_cmp2.avi_annotations/stop_1323804592.avi_image7.png' ) img = cv2.imread( 'input/lisa/vid0/frameAnnotations-vid_cmp2.avi_annotations/pedestrian_1323804492.avi_image8.png' ) img = cv2.imread( 'input/lisa/vid0/frameAnnotations-vid_cmp2.avi_annotations/pedestrian_1323804492.avi_image5.png' ) # img = cv2.imread('input/lisa/vid0/frameAnnotations-vid_cmp2.avi_annotations/pedestrian_1323804463.avi_image3.png') saved_hist_results = getFiltersHistograms(INPUT_FILTER) img_in = np.copy(img) classifier, hog, svm_dict = saved_svm_or_fit(path='svms_cur') bin_classifier, hog, svm_binary_dict = saved_svm_or_fit_bin( path='svms_bin_cur') hog = getHogDescriptor((32, 32)) detectors = [HAARDetection('data_stop'), HAARDetection('data_diamond')] print(img.shape) possible_regions = [] for detector in detectors: possible_regions.extend(detector.find_signs(img_in)) # print(possible_regions) img_in = np.copy(img) candidate_signs = [] candidate_borders = [] for (x, y, w, h) in possible_regions: just_box = img[y:y + h, x:x + w, :] box_h, box_w, _ = just_box.shape if box_h == 0 or box_w == 0: continue # print('box shape', just_box.shape) # print(x,x+w, y, y+h) # just_box = svm_preprocessing(just_box) bin_res = classify(bin_classifier, hog, [just_box], svm_binary_dict) if bin_res[0] == 'sign': print('sign') cv2.rectangle(img_in, (x, y), (x + w, y + h), (0, 100, 255), 2) candidate_signs.append(just_box) candidate_borders.append((x, y, w, h)) compareHistogram(just_box, saved_hist_results) # candidate_signs.append(just_box) # candidate_signs.append(just_box) # candidate_signs.append(just_box) # candidate_signs.append(just_box) # font = cv2.FONT_HERSHEY_SIMPLEX else: cv2.rectangle(img_in, (x, y), (x + w, y + h), (255, 255, 0), 2) # show_img(img_in) # cv2.putText(img, 'Sign',(x-w,y-h), font, 0.5, (11,255,255), 2, cv2.LINE_AA) show_img(img_in) # print(type(candidate_signs)) if len(candidate_signs) > 0: # show_img(region[0]) classification = classify(classifier, hog, candidate_signs, svm_dict) draw_classification(img, candidate_borders, classification) print(possible_regions[0].shape)
def stop_sign_detection(img_in): """Finds the centroid coordinates of a stop sign in the provided image. Args: img_in (numpy.array): image containing a traffic light. Returns: (x,y) tuple of the coordinates of the center of the stop sign. """ # from tempfile import TemporaryFile # outfile = TemporaryFile() temp_img = np.copy(img_in) cimg = np.copy(img_in[:, :, 2]) # np.save(outfile, cimg) np.putmask(cimg, np.logical_or(cimg > 245, cimg < 200), 0) show_img(cimg) cimg = cv2.medianBlur(cimg, 11) # cv2.blur(cimg,(10,10)) # gray = cv2.cvtColor(temp_img,cv2.COLOR_BGR2GRAY) edges = cv2.Canny(cimg, 50, 150, apertureSize=3) show_img(edges) minLineLength = 10 maxLineGap = 7 lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 30, minLineLength, maxLineGap) mid_x, mid_y = 0, 0 stop_sign = None if lines is not None and lines.shape[1] > 8: #print 'lines', lines lines = lines[0] labels = run_kmeans(lines) zeros = lines[labels.ravel() == 0] ones = lines[labels.ravel() == 1] if len(zeros) == 8: #print 'stop sign detected' stop_sign = zeros elif len(ones) == 8: #print 'stop sign detected' stop_sign = ones temp_img = drawLinesP(temp_img, np.asarray([lines])) # lines, temp_img = detectLinesP(img_in) show_img(temp_img) # temp_img = np.copy(img_in[:,:,2]) # cimg = cv2.cvtColor(temp_img,cv2.COLOR_BGR2GRAY) # cimg = cv2.medianBlur(cimg,3) # cimg = cv2.GaussianBlur(temp_img,(5,5),0) # show_img(cimg) # #print cimg cimg = np.copy(img_in[:, :, 2]) # np.save(outfile, cimg) np.putmask(cimg, np.logical_or(cimg > 245, cimg < 200), 0) cimg = cv2.medianBlur(cimg, 51) circles = cv2.HoughCircles(cimg, cv2.HOUGH_GRADIENT, 1, 20, param1=30, param2=15, minRadius=0, maxRadius=50) if circles is not None: circles = np.uint16(np.around(circles)) #print 'circles:', circles # circles = circles[0][-1] # mid_x = circles[1] # mid_y = circles[0] # pixelSize = 12 # right_x, left_x = mid_x - pixelSize, mid_x + pixelSize # top_y, bottom_y = mid_y - pixelSize, mid_y + pixelSize # #print right_x, left_x, top_y, bottom_y # red = img_in[right_x:left_x, top_y:bottom_y, 2] # green = img_in[right_x:left_x, top_y:bottom_y, 1] # blue = img_in[right_x:left_x, top_y:bottom_y, 0] # # # #print red # # #print cimg[right_x:left_x, top_y:bottom_y] # show_img(img_in[right_x:left_x, top_y:bottom_y,:]) # show_img(cimg[right_x:left_x, top_y:bottom_y]) # #print np.asarray([lines[0][0],lines[0][2]]) temp_img = draw_circles(temp_img, circles[0]) else: print('no circle') # show_img(temp_img) # temp_img = np.copy(img_in) # temp_img = drawLinesP(temp_img, zeros) # show_img(temp_img) # # # temp_img = np.copy(img_in) # temp_img = drawLinesP(temp_img, ones) # show_img(temp_img) # temp_img = draw_circles(img_in, circles[0]) # temp_img = detectLines(img_in) show_img(temp_img) if circles is not None: mid_x, mid_y = circles[0][0][:2] return mid_x, mid_y
from darknet_yolo.darknet_pytorch import Darknet from utils import is_roi_outside_frame, show_img detect = Darknet() for file in glob.glob("resources/3d_screenshots/*"): frame = cv2.imread(file) # frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE) # frame = cv2.flip(frame, 1) frame_width = frame.shape[1] frame_height = frame.shape[0] frame_copy = frame.copy() ######### DETECTION detections_list = detect.yolo_detection(frame_copy) show_img("3D model image", frame_copy) for detection in detections_list: if detection[0] == 'painting' and not is_roi_outside_frame( frame_width, frame_height, *detection[2:6]): print("[INFO] Painting detected: " + file) left = detection[2] # x top = detection[3] # y right = detection[4] # x + w bottom = detection[5] # y + h painting = frame[int(top * 0.75):int(bottom), int(left):int(right)] show_img("3D model image", painting) retrieve = RetrieveClass() rectify = RectifyClass(retrieve)
def evaluate(net, X, Y, plot_name): """Evaluate a single batch (without training).""" inp_seq_len = X.size(0) outp_seq_len, batch_size, _ = Y.size() # New sequence net.init_sequence(batch_size) utils.show_img(net.memory.memory.data.squeeze(0).numpy(), 'mem') # Feed the sequence + delimiter w_enc = {'read': [], 'write': []} print('-' * 20 + 'encoding' + '-' * 20) for i in range(inp_seq_len): o, state = net(X[i]) utils.show_img(net.memory.memory.data.squeeze(0).numpy(), 'mem') w_enc['read'].append(state[-1][0]) w_enc['write'].append(state[-1][1]) # Read the output (no input given) w_dec = {'read': [], 'write': []} print('-' * 20 + 'decoding' + '-' * 20) y_out = Variable(torch.zeros(Y.size())) for i in range(outp_seq_len): y_out[i], state = net() utils.show_img(net.memory.memory.data.squeeze(0).numpy(), 'mem') w_dec['read'].append(state[-1][0]) w_dec['write'].append(state[-1][1]) w_enc_read = torch.cat(w_enc['read']) w_enc_write = torch.cat(w_enc['write']) w_dec_read = torch.cat(w_dec['read']) w_dec_write = torch.cat(w_dec['write']) boundary_enc = torch.Tensor(np.array([[0.5] * w_enc_write.shape[0]])) boundary_dec = torch.Tensor(np.array([[0.5] * w_dec_write.shape[0]])) # w = torch.cat( # [w_enc_read, # boundary, # w_enc_write, # boundary, # w_dec_read, # boundary, # w_dec_write]) w_enc = torch.cat( [w_enc_read, boundary_enc.transpose(0, 1), w_enc_write], dim=1) w_dec = torch.cat( [w_dec_read, boundary_dec.transpose(0, 1), w_dec_write], dim=1) boundary = torch.Tensor(np.array([[0.5] * 41])) w = torch.cat([w_enc, boundary, w_dec]) # utils.show_img(w.data.numpy(), plot_name) # utils.show_img(w_enc.data.numpy(), plot_name) # utils.show_img(w_dec.data.numpy(), plot_name) # utils.show_img(w_enc_read) # utils.show_img(w_enc_write) # utils.show_img(w_dec_read) # utils.show_img(w_dec_write) y_out_binarized = y_out.clone().data y_out_binarized.apply_(lambda x: 0 if x < 0.5 else 1) # The cost is the number of error bits per sequence cost = torch.sum(torch.abs(y_out_binarized - Y.data)) result = { 'cost': cost / batch_size, 'y_out': y_out, 'y_out_binarized': y_out_binarized, # 'states': states } return result
def detect(self, frame): ''' input: frame output: dst, box(xyxy(bbox) or 4x2(min_area)), center(xy) ''' if frame is None or frame is []: raise TypeError('No frame input') # Resize the frame shape = self.shape img0 = np.copy(frame) H, W, C = frame.shape scale_factor = np.array([shape[0]/W, shape[1]/H]) frame = cv2.resize(frame, shape) # 300,400,3 # Gaussian Blur frame_gaussian = cv2.GaussianBlur(frame, (7, 7), 0) # RGB to HSV frame_hsv = cv2.cvtColor(frame_gaussian, cv2.COLOR_BGR2HSV) # Get mask according to HSV hsv_thres_values = self.get_trackbar_value() if self.debug else list(self.icol) mask = cv2.inRange(frame_hsv, np.array(hsv_thres_values[:3]), np.array(hsv_thres_values[3:])) # Median filter mask_f = cv2.medianBlur(mask, 5) # Morphology for three times kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)) mask_m = cv2.morphologyEx(mask_f, cv2.MORPH_CLOSE, kernel) mask_m = cv2.morphologyEx(mask_m, cv2.MORPH_OPEN, kernel) # Get Contours of The Mask box = None # xyxy center = None # xy _, contours, _= cv2.findContours(mask_m, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cnt_list = [cnt for cnt in contours if self.contours_thres[0]<len(cnt)<self.contours_thres[1]] if cnt_list: cnt = max(contours, key=lambda x: x.shape[0]) # mutiple contours, choose the biggest one if self.box_type == 'bbox': # Get Bounding Box box = np.int0(scale_bbox(xywh2xyxy(cv2.boundingRect(cnt)), 1/scale_factor)) # xyxy center = np.int0(np.array([(box[0] + box[2])/2, (box[1] + box[3])/2])) elif self.box_type == 'min_area': # Get Minimum Area Box rect = cv2.minAreaRect(cnt) # center(x, y), (width, height), angle of rotation box = cv2.boxPoints(rect) # (4, 2) # scale box box = box / scale_factor center = np.sum(box, axis=0)/4 box, center = np.int0(box), np.int0(center) else: raise TypeError('unsupported box type %s' % self.box_type) # Result dst = self.plot_img(img0, box) # view result if self.view_result: show_img(self.window_name, cv2.resize(dst, shape)) # show_img(self.window_name, dst return dst, box, center
horizontal_flip = np.random.randint(0, 2) if horizontal_flip == 1: flip = True else: flip = False datagen = ImageDataGenerator( rotation_range=20, width_shift_range=0.2, height_shift_range=0.2, horizontal_flip=flip, shear_range=0.1, zoom_range=[0.9, 1.25] ) nat_cmnr = datagen.flow(x_train, y_train, batch_size=len(x_train)) x_batch, y_batch = next(nat_cmnr) return np.array(x_batch), np.array(y_batch) if __name__=='__main__': dataset = DataGenerator(TEST_SUMMARY_PATH) for i in range(1): gen = dataset.data_generator(training=True, img_augmentation=True) x_batch, y_batch = next(gen) print(np.shape(x_batch)) for i in range(np.shape(y_batch)[0]): show_img(x_batch[i], title=str(y_batch[i]))
img = Image.merge("RGB", (b, g, r)) img = img.rotate(angle=rotation, expand=False) return np.array(img) def read_horizontal_image(path): img = cv2.imread(path) lines = get_all_lines(img) angle = get_horiz_angle(lines) return get_rotated(path, angle) if __name__ == "__main__": from utils import get_pickle from tqdm.auto import tqdm GT = get_pickle("datasets/angles_qsd1w5_v2.pkl") sum_err, elems = 0, 0 for i, path in enumerate(tqdm(image_paths)): img = cv2.imread(path) lines = get_all_lines(img) angle = get_horiz_angle(lines) gt_like_angle = get_GTFORMAT_angle(angle) for gt in GT[i]: sum_err += angle_diff(gt, gt_like_angle, mod=180) elems += 1 show_img(draw_horizontal_lines(img, lines, angle)) show_img(get_rotated(path, angle)) print(f"Average precision {sum_err / elems}")
def find_poly(img, opened, side_num=6): ''' Function: get contour of poly with side num Input Params: - img: original image - opened: open image of original image - side_num: side num of poly to be detected, default 6 Output Params: - contours: contour of poly ''' # get contours image, contours, hierarchy = cv2.findContours(opened, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # check contours detected (if need) contour_img = np.zeros(image.shape, np.uint8) contour_img.fill(255) cv2.drawContours(contour_img, contours, -1, (0, 0, 0), 2) show_img(contour_img, 'contour img') cv2.waitKey(0) cv2.destroyAllWindows() # coutour wash # find max area contour area = [] for cnt in contours: area.append(cv2.contourArea(cnt)) max_index = np.argmax(np.array(area)) print("hierarchy: ", hierarchy.shape) print(hierarchy) # begin width search indexs = [max_index] indexs_queue = [max_index] while len(indexs_queue) != 0: parent_index = indexs_queue[0] indexs_queue.pop(0) # add child index child_index = hierarchy[0][parent_index][2] while child_index != -1: indexs_queue.append(child_index) indexs.append(child_index) child_index = hierarchy[0][child_index][0] indexs = np.unique(np.array(indexs)) # unique print("indexs: ", indexs) wash_contours = [] for i, cnt in enumerate(contours): if i in indexs: wash_contours.append(cnt) # check wash contours, if needed wash_contour_img = get_blank_img(image.shape) cv2.drawContours(wash_contour_img, wash_contours, -1, (0, 0, 0), 3) show_img(wash_contour_img, 'wash contours') cv2.waitKey(0) cv2.destroyAllWindows() # approx contours approx_contours = [] for i, cnt in enumerate(wash_contours): epsilon = find_epsilon(side_num) * cv2.arcLength(cnt, True) approx = cv2.approxPolyDP(cnt, epsilon, True) # check approx contour if needed # print("approx: ",approx) # temp_img = get_blank_img(image.shape) # cv2.drawContours(temp_img,[approx],-1,(0,0,0),4) # show_img(temp_img,'approx contour') # cv2.waitKey(0) if side_num == 0: # circle if len(approx) < 10: # core pass else: approx_contours.append(approx) else: # poly if len(approx) != side_num: # core pass else: approx_contours.append(approx) # check contours detected (if need) # approx_contour_img = np.zeros(image.shape,np.uint8) # approx_contour_img.fill(255) # cv2.drawContours(approx_contour_img,approx_contours,-1, (0,0,0), 3) # cv2.imshow('approx contour img',approx_contour_img) # cv2.waitKey(0) # cv2.destroyAllWindows() print("find {} side poly: {}".format(side_num, len(approx_contours))) return approx_contours
from model import FashionNet import sys import mxnet as mx from mxnet import nd from data_loader import read_img from utils import show_img import numpy as np if __name__ == '__main__': model_file = sys.argv[1] img_file = sys.argv[2] net = FashionNet() ctx = mx.cpu() net.collect_params().load(model_file, ctx=ctx) img, height, width = read_img(img_file) img = nd.array(img).reshape([1, 3, 224, 224]) l, vs, classifier_output, _, _ = net(img) out = classifier_output[0].asnumpy() print(np.where(out > 0)) landmarks = l[0].asnumpy() landmarks = landmarks.reshape([len(landmarks) // 2, 2]) print(landmarks) show_img(img_file, landmarks)
# frame = frame_origin[180:400, 120:500] now = datetime.datetime.now() # date_stamp = (f'{now.year}.{now.month}.{now.day}._{now.hour}h{now.minute}m{now.second}s') date_stamp = (f'{current_iter}') # utils.show_img(frame, 'Imagem') # utils.save_img(frame, 'Cropped') # ------------------------------------------------------------------------------------------- Gamma try: frame_gamma = utils.adjust_gamma(frame, gamma=2.0) except: print('Erro: Gamma') utils.show_img(frame_gamma, 'Gamma') # utils.save_img(frame, 'Gamma', date_stamp) # ------------------------------------------------------------------------------------------- CLACHE try: frame_clache = utils.apply_clache(frame_gamma, clip_Limit=2.0, GridSize=8) except: print('Erro: Clache') # utils.show_img(frame_clache, 'Clache') # utils.save_img(frame, 'Clache', date_stamp) # ------------------------------------------------------------------------------------------- K-MEAN try:
except: print('Erro: Carregar imagem') # -------------------------------------------- Camera input # try: # frame_origin = camera_capture(0) # except: # print('Erro: Captura da camera') # frame = frame_origin[180:400, 120:500] now = datetime.datetime.now() # date_stamp = (f'{now.year}.{now.month}.{now.day}._{now.hour}h{now.minute}m{now.second}s') date_stamp = (f'{current_iter}') utils.show_img(frame, 'Imagem') utils.save_img(frame, 'Original') # ------------------------------------------------------------------------------------------- Gamma frame_gamma = utils.adjust_gamma(frame, gamma=2.0) utils.show_img(frame_gamma, 'Gamma') utils.save_img(frame_gamma, 'Gamma', date_stamp) # ------------------------------------------------------------------------------------------- CLAHE frame_clache = utils.apply_clache( frame_gamma, clip_Limit=2.0, GridSize=8) utils.show_img(frame_clache, 'Clache') utils.save_img(frame_clache, 'Clache', date_stamp)
import utils from generator import Generator import os if __name__ == '__main__': config, unparsed = get_config() if len(unparsed) > 0: print_usage() exit(1) # Generate images model = Generator(config) weights_file = config.weights_dir if os.path.exists(weights_file): device = "cuda:0" if torch.cuda.is_available() else "cpu" load_res = torch.load(weights_file, map_location=device) model.load_state_dict(load_res) model.eval() inp_list = sorted(os.listdir(config.data_dir)) for i in range(len(inp_list)): img = mpimg.imread(config.data_dir + inp_list[i]) print(img.shape) img = utils.image2tensor(img) masks, f1, f2, x = model.forward(img) mask = utils.transform_to_mpimg(masks[-1]) mask = mask.squeeze(2) utils.show_img(mask) exit(0) # x = x.detach().numpy() # x = x.squeeze(0).transpose(1, 2, 0).astype(np.uint8)
def detect_and_classify(images, svm_paths, svm_bin_path, haar_paths, showImg=False, labels=None): classifiers_dict = get_saved_svms(svm_paths) bin_classifier, bin_hog, svm_binary_dict = saved_svm_or_fit_bin( path=svm_bin_path) saved_hist_results_in = getFiltersHistograms(INPUT_FILTER_IN) saved_hist_results_out = getFiltersHistograms(INPUT_FILTER_OUT) detectors = [] for haar_path in haar_paths: detectors.append(HAARDetection(haar_path)) possible_regions = [] output_images = [] total_results = {} if not classifiers_dict: print('classifier does not exist, check path ') return print('num of images to process', len(images)) num = 0 for img in images: img_in = np.copy(img) # denoising(img) for detector in detectors: possible_regions.extend(detector.find_signs(img_in)) # candidate_borders, candidate_signs = filter_empty_spaces(possible_regions) possible_signs = [] candidate_borders = [] # img_denoise = cv2.medianBlur(img, 3) img_denoise = cv2.fastNlMeansDenoisingColored(img, None, 7, 7, 7, 21) for (x, y, w, h) in possible_regions: just_box = img_denoise[y:y + h, x:x + w, :] box_h, box_w, _ = just_box.shape if box_h == 0 or box_w == 0: continue # print('box shape', just_box.shape) # print(x,x+w, y, y+h) bin_res = classify(bin_classifier, bin_hog, [just_box], svm_binary_dict) if bin_res[0] == 'sign': cv2.rectangle(img_in, (x, y), (x + w, y + h), (0, 255, 255), 2) # just_box_bright = make_imgs_brighter(just_box) corr_res_in = compareHistogram(just_box, saved_hist_results_in) corr_res_out = compareHistogram(just_box, saved_hist_results_out) # print('out', corr_res_out) # print('in', corr_res_in) if np.any(corr_res_in > 0.50) and np.all(corr_res_out < 0.70): possible_signs.append(just_box) candidate_borders.append((x, y, w, h)) else: cv2.rectangle(img_in, (x, y), (x + w, y + h), (100, 0, 255), 2) # show_img(just_box) # candidate_signs.append(just_box) # candidate_signs.append(just_box) # candidate_signs.append(just_box) # candidate_signs.append(just_box) # font = cv2.FONT_HERSHEY_SIMPLEX # else: # cv2.rectangle(img_in,(x,y),(x+w,y+h),(255,255,0),2) # show_img(img_in) # cv2.putText(img, 'Sign',(x-w,y-h), font, 0.5, (11,255,255), 2, cv2.LINE_AA) if labels: img_name = os.path.basename(labels[num]) else: img_name = 'img_%d' % num num += 1 # show_img(img_in, img_name) draw_img = np.copy(img) try: # show_img(region[0]) if len(possible_signs) > 0: print(img_name) possible_signs = np.array(possible_signs) candidate_borders = np.array(candidate_borders) for classifier, hog, label_dict in classifiers_dict.values(): if classifier is None or hog is None: print('svm classifiers not setup correctly') continue if len(possible_signs) > 0: # print('entering again') classification = classify(classifier, hog, possible_signs, label_dict) classification = np.array(classification) print(classification) draw_img = draw_classification(draw_img, candidate_borders, classification) # print(possible_signs.shape) # found_signs = possible_signs[classification!='nosign'] found_borders = candidate_borders[ classification != 'nosign'] found_classifications = classification[ classification != 'nosign'] possible_signs = possible_signs[classification == 'nosign'] candidate_borders = candidate_borders[classification == 'nosign'] assert len(found_borders) == len( found_classifications ), 'classifications and border do not match' for clf, border in zip(found_classifications, found_borders): if img_name in total_results: total_results[img_name].append( (clf, border[0], border[1], border[0] + border[2], border[1] + border[3])) else: total_results[img_name] = [ (clf, border[0], border[1], border[0] + border[2], border[1] + border[3]) ] # print(len(possible_signs)) # print(len(candidate_borders)) # no signs were found by both the classifiers if img_name not in total_results: total_results[img_name] = [] output_images.append(draw_img) else: output_images.append(draw_img) if not img_name in total_results: total_results[img_name] = [] except Exception as e: print(e) print(img_name) if showImg: show_img(draw_img) return output_images, total_results
# Read until video is completed while cap.isOpened(): ret, frame = cap.read() scale_percent = 20 # percent of original size width = int(frame_width * scale_percent / 100) height = int(frame_height * scale_percent / 100) dim = (width, height) # resize image frame = cv2.resize(frame, dim, interpolation=cv2.INTER_AREA) if ret == True: contours, hierarchy = painting_detection.contours( image_segmentation(frame), adaptive=True) if len(contours) != 0: painting_detection.dram_multiple_contours(frame, contours, approximate=False) show_img("Frame", frame) # Break the loop else: break # When everything done, release the video capture and writer objects cap.release() # Closes all the frames cv2.destroyAllWindows()
def decode_output(y_pred, shape, grid_size): y_pred[..., 0:2] = tf.sigmoid(y_pred[..., 0:2]) y_pred[..., 4] = tf.sigmoid(y_pred[..., 4]) box = decode_bounding_box(y_pred[0], shape, grid_size) return box def load_data(data_folder='./vietnam/'): files = glob.glob(data_folder + '/*.jpg') return files if __name__ == '__main__': files = load_data() print(files) model = TiniYOLO() model = model.model(training=False) model.load_weights('big_img_check_point/cp{}.h5'.format(400)) for sample in files: sample = cv2.imread(sample) sample = transform_img(sample) sample_shape = sample.shape result = np.expand_dims(sample, 0) result = model.predict(result) result = decode_output(result, sample_shape, GRID_SIZE) img = draw_bbox(sample, result) show_img(img, str(sample_shape)) print(result)