def weightVideo(vidName): vidFile = cv2.VideoCapture(vidName) nFrames = int(vidFile.get(cv.CV_CAP_PROP_FRAME_COUNT)) fps = vidFile.get(cv.CV_CAP_PROP_FPS) waitPerFrameInMillisec = int(1 / fps * 1000 / 1) print 'Num. Frames = ', nFrames print 'Frame Rate = ', fps, ' frames per sec' s, background = vidFile.read() shape = background.shape background = cv2.cvtColor(background, cv2.COLOR_RGB2GRAY) background = background / 255.0 mu = 0.01 for f in xrange(nFrames - 1): s, frameImg = vidFile.read() gray = cv2.cvtColor(frameImg, cv2.COLOR_RGB2GRAY) gray = gray / 255.0 background = (1 - mu) * background + mu * gray # for i in range(shape[0]): # print i # for j in range(shape[1]): # z = background[i,j] # background[i,j] = sigmoid(z*1.0) # print background # background=cv2.cvtColor(background,cv2.CV_8U) cv2.imshow("Original Frame", utils.rotateImg(frameImg)) cv2.imshow("My Video Window", utils.rotateImg(background)) cv2.waitKey(waitPerFrameInMillisec)
def weightVideo(vidName): vidFile = cv2.VideoCapture(vidName) nFrames = int(vidFile.get(cv.CV_CAP_PROP_FRAME_COUNT) ) fps = vidFile.get(cv.CV_CAP_PROP_FPS ) waitPerFrameInMillisec = int( 1/fps * 1000/1 ) print 'Num. Frames = ', nFrames print 'Frame Rate = ', fps, ' frames per sec' s,background=vidFile.read() shape = background.shape background=cv2.cvtColor(background,cv2.COLOR_RGB2GRAY) background = background/255.0 mu = 0.01 for f in xrange( nFrames -1 ): s,frameImg = vidFile.read() gray = cv2.cvtColor(frameImg,cv2.COLOR_RGB2GRAY) gray = gray / 255.0 background = (1-mu) * background + mu * gray # for i in range(shape[0]): # print i # for j in range(shape[1]): # z = background[i,j] # background[i,j] = sigmoid(z*1.0) # print background # background=cv2.cvtColor(background,cv2.CV_8U) cv2.imshow( "Original Frame", utils.rotateImg(frameImg)) cv2.imshow( "My Video Window", utils.rotateImg(background)) cv2.waitKey( waitPerFrameInMillisec )
def flipPasteImage(self, paste_img, labels): flip_val = np.random.randint(0, 100) if flip_val < 50: logging.info("Rotating by -90.") rotate_deg = -90 else: logging.info("Rotating by 90.") rotate_deg = 90 rotated_paste_img, = utils.rotateImg(paste_img, rotate_deg) new_labels = [] for l in labels: new_right = utils.rotate([0.0, 0.0], l['x'], rotate_deg) new_top = utils.rotate([0.0, 0.0], l['y'], rotate_deg) new_left = utils.rotate([0.0, 0.0], l['x'] + l['width'], rotate_deg) new_bottom = utils.rotate([0.0, 0.0], l['y'] + l['height'], rotate_deg) new_l = l.copy() new_l['x'] = new_right new_l['y'] = new_top new_l['width'] = new_left - new_right new_l['height'] = new_bottom - new_top new_labels.append(new_l) self.printLabelDims(new_labels) return rotated_paste_img, new_labels
def rotateCanvasImage(self, canvas_img, rotate_deg=0): if rotate_deg == 0: rotate_deg = self.getForcedRandomRotationValue( self.max_canvas_rotation) logging.info(" rotate_deg: {}.".format(rotate_deg)) rotated_canvas_img, rotated_canvas_mask = utils.rotateImg(canvas_img, rotate_deg, mask_in=None) # utils.showAndWait('rotated_paste_img', rotated_canvas_img) if rotated_canvas_img.shape[0] != self.final_img_height or \ rotated_canvas_img.shape[1] != self.final_img_width: left_idx = int(rotated_canvas_img.shape[1] / 2.0 - self.final_img_width / 2.0) right_idx = left_idx + self.final_img_width top_idx = int(rotated_canvas_img.shape[0] / 2.0 - self.final_img_height / 2.0) bottom_idx = top_idx + self.final_img_height rotated_canvas_img = rotated_canvas_img[top_idx:bottom_idx, left_idx:right_idx] return rotated_canvas_img
# background *= 1./255 print background.shape background = cv2.cvtColor(background, cv2.COLOR_RGB2GRAY) background = background / 255.0 print background.shape print np.amax(background) mu = 0.05 for f in xrange(nFrames): s, frameImg = vidFile.read() if f % 10 == 0: gray = cv2.cvtColor(frameImg, cv2.COLOR_RGB2GRAY) retinexed = np.zeros([gray.shape[0], gray.shape[1], 3]) for i in range(2): retinexed[:, :, i] = gray retinexed = cca.retinex(retinexed) retinexed = cv2.cvtColor(retinexed, cv2.cv.CV_BGR2GRAY) cv2.imshow("retinexed frame", retinexed) retinexed = retinexed / 255.0 background = (1 - mu) * background + mu * retinexed # print background # background=cv2.cvtColor(background,cv2.CV_8U) cv2.imshow("Original Frame", utils.rotateImg(frameImg)) cv2.imshow("My Video Window", utils.rotateImg(background)) cv2.waitKey(waitPerFrameInMillisec) # # When playing is done, delete the window # # NOTE: this step is not strictly necessary, # # when the script terminates it will close all windows it owns anyways # cv2.DestroyWindow( "My Video Window" )
def rotatePasteImage(self, img, labels, rotate_deg=0): # drawn_img = self.drawLabels(img.copy(), labels) # utils.showAndWait('drawn_img', drawn_img) rotated, mask = utils.rotateImg(img, rotate_deg) # rotated = misc.imrotate(img, rotate_deg) # utils.showAndWait('rotated', rotated) center_x = img.shape[1] / 2.0 center_y = img.shape[0] / 2.0 new_center_x = rotated.shape[1] / 2.0 new_center_y = rotated.shape[0] / 2.0 new_labels = [] for l in labels: w = l['width'] # * 0.85 h = l['height'] # * 0.85 tl = [l['x'] - center_x, (l['y'] - center_y)] tr = [l['x'] + w - center_x, (l['y'] - center_y)] bl = [l['x'] - center_x, (l['y'] + h - center_y)] br = [l['x'] + w - center_x, (l['y'] + h - center_y)] top_left = utils.rotate([0.0, 0.0], tl, -rotate_deg) top_right = utils.rotate([0.0, 0.0], tr, -rotate_deg) bottom_left = utils.rotate([0.0, 0.0], bl, -rotate_deg) bottom_right = utils.rotate([0.0, 0.0], br, -rotate_deg) tl2 = [(top_left[0] + new_center_x), (top_left[1] + new_center_y)] tr2 = [(top_right[0] + new_center_x), (top_right[1] + new_center_y)] bl2 = [(bottom_left[0] + new_center_x), (bottom_left[1] + new_center_y)] br2 = [(bottom_right[0] + new_center_x), (bottom_right[1] + new_center_y)] # rotated = cv2.line(rotated, # (int(tl2[0]), int(tl2[1])), # (int(tr2[0]), int(tr2[1])), # color=(0, 0, 255), thickness=3) # rotated = cv2.line(rotated, # (int(tr2[0]), int(tr2[1])), # (int(br2[0]), int(br2[1])), # color=(0, 0, 255), thickness=3) # rotated = cv2.line(rotated, # (int(br2[0]), int(br2[1])), # (int(bl2[0]), int(bl2[1])), # color=(0, 0, 255), thickness=3) # rotated = cv2.line(rotated, # (int(bl2[0]), int(bl2[1])), # (int(tl2[0]), int(tl2[1])), # color=(0, 0, 255), thickness=3) # utils.showAndWait('rotated', rotated) min_x = min(tl2[0], tr2[0], bl2[0], br2[0]) max_x = max(tl2[0], tr2[0], bl2[0], br2[0]) min_y = min(tl2[1], tr2[1], bl2[1], br2[1]) max_y = max(tl2[1], tr2[1], bl2[1], br2[1]) new_l = l.copy() new_l['x'] = min_x new_l['y'] = min_y new_l['width'] = (max_x - min_x) new_l['height'] = (max_y - min_y) new_labels.append(new_l) utils.printLabelDims(new_labels) return rotated, new_labels
def addPastedImages(self, canvas_img_file, canvas_img, paste_img_files, paste_label_dir, save_img_dir, save_label_dir, canvas_idx, tile_idx, out_labels): """ Adds paste images to the canvas file and saves it and the labels. :param canvas_img_file: canvas image filename to split :param canvas_img: canvas image to split :param paste_img_files: paste image files. :param width_multiple: multiple of width to final image size. :param height_multiple: multiple of height to final image size. :param canvas_idx: canvas index. :param tile_idx: tile index. """ canvas_width = canvas_img.shape[1] canvas_height = canvas_img.shape[0] num_pastes = np.random.randint(self.min_pasted_per_canvas, self.max_pasted_per_canvas) labels = [] if canvas_height != self.final_img_height or canvas_width != self.final_img_width: logging.error( "The canvas height for a paste add does not match the final image dimensions. Skipping image." ) return canvas_label = np.zeros([canvas_height, canvas_width, 3], dtype=np.uint8) logging.info("num_pastes: {}".format(num_pastes)) for past_idx in range(num_pastes): paste_img_file_idx = self.paste_image_idx # paste_img_file = '/media/dcofer/Ubuntu_Data/drone_images/drones/111.png' paste_img_file = paste_img_files[paste_img_file_idx] paste_filename = os.path.basename(paste_img_file) paste_basename = os.path.splitext(paste_filename)[0] logging.info(" Pasting in {}".format(paste_img_file)) logging.info(" Paste Image Idx {}".format(paste_img_file_idx)) paste_img = self.loadPasteImage(paste_img_file) # utils.showAndWait('paste_img', paste_img) paste_label_file = paste_label_dir + '/' + paste_basename + '_label.png' paste_label_img = cv2.imread(paste_label_file, cv2.IMREAD_UNCHANGED) paste_label_img = cv2.cvtColor(paste_label_img, cv2.COLOR_BGR2GRAY) # utils.showAndWait('paste_label_img', paste_label_img) paste_width = paste_img.shape[1] paste_height = paste_img.shape[0] logging.info(" paste_width: {}".format(paste_width)) logging.info(" paste_height: {}".format(paste_height)) if paste_label_img.shape[ 0] != paste_height or paste_label_img.shape[ 1] != paste_width: raise RuntimeError( "Paste label dims do not match paste image.") new_paste_width, new_paste_height = self.calcNewPasteDims( paste_width, paste_height) logging.info(" new_paste_width: {}".format(new_paste_width)) logging.info(" new_paste_height: {}".format(new_paste_height)) if paste_width != new_paste_width or paste_height != new_paste_height: sized_paste_img = cv2.resize(paste_img, dsize=(new_paste_width, new_paste_height), interpolation=cv2.INTER_AREA) sized_paste_mask = cv2.resize(paste_label_img, dsize=(new_paste_width, new_paste_height), interpolation=cv2.INTER_AREA) else: sized_paste_img = paste_img sized_paste_mask = paste_label_img # utils.showAndWait('sized_paste_img', sized_paste_img) # utils.showAndWait('sized_paste_mask', sized_paste_mask) flip_val = np.random.randint(0, 100) if flip_val < 50: logging.info( " flip_val: {}. Flipping image.".format(flip_val)) flipped_paste_img = np.fliplr(sized_paste_img) flipped_paste_mask = np.fliplr(sized_paste_mask) else: logging.info( " flip_val: {}. Leaving image unflipped".format( flip_val)) flipped_paste_img = sized_paste_img flipped_paste_mask = sized_paste_mask rotate_deg = int( np.random.uniform(-self.max_paste_rotation, self.max_paste_rotation)) logging.info(" rotate_deg: {}.".format(rotate_deg)) rotated_paste_img, rotated_paste_mask = utils.rotateImg( flipped_paste_img, rotate_deg, mask_in=flipped_paste_mask) # utils.showAndWait('rotated_paste_img', rotated_paste_img) # utils.showAndWait('rotated_paste_mask', rotated_paste_mask) paste_width = rotated_paste_img.shape[1] paste_height = rotated_paste_img.shape[0] paste_x, paste_y = self.getNonoverlappingPastePos( paste_width, paste_height, canvas_width, canvas_height, labels) if paste_x < 0 or paste_y < 0: break # paste_x = 1081 # paste_y = 266 logging.info(" paste_x: {}".format(paste_x)) logging.info(" paste_y: {}".format(paste_y)) # Get canvas ROI canvas_roi = canvas_img[paste_y:(paste_y + paste_height), paste_x:(paste_x + paste_width)] #canvas_roi = np.zeros([paste_height, paste_width, 3], dtype=np.uint8) # showAndWait('canvas_roi', canvas_roi) # Regnerate a new mask because the one that was put through all the processing is not # intact anymore. This was causing weird artifacting. ret, mask = cv2.threshold(rotated_paste_mask, 5, 255, cv2.THRESH_BINARY) # mask = new_mask[:, :, 0] mask_inv = cv2.bitwise_not(mask) # utils.showAndWait('mask', mask) # Black out the are of the mask. background_roi = cv2.bitwise_and(canvas_roi, canvas_roi, mask=mask_inv) # showAndWait('background_roi', background_roi) # cv2.imwrite('/media/dcofer/Ubuntu_Data/drone_images/canvas_ros.png', background_roi) # Now randomly change brightness and contract of foreground drone bright_rand = np.random.randint(0, 100) if bright_rand < self.blur_thresh: logging.info( " bright_rand: {}. Adjusting brightness/contrast.". format(bright_rand)) bright_val = np.random.randint(-self.bright_max, self.bright_max) contrast_val = np.random.normal(1.0, self.contrast_max) if contrast_val < 0.5: contrast_val = 0.7 if contrast_val > 1.5: contrast_val = 1.3 logging.info(" bright_val: {}".format(bright_val)) logging.info(" contrast_val: {}".format(contrast_val)) bright_foreground_img = cv2.convertScaleAbs(rotated_paste_img, alpha=contrast_val, beta=bright_val) # utils.showAndWait('rotated_paste_img', rotated_paste_img) # utils.showAndWait('bright_foreground_img', bright_foreground_img) else: logging.info( " bright_rand: {}. Leaving image brightness/contrast alone" .format(bright_rand)) bright_foreground_img = rotated_paste_img # Now take only region of paste image that is not black foreground_roi = cv2.bitwise_and(bright_foreground_img, bright_foreground_img, mask=mask) # showAndWait('foreground_roi', foreground_roi) # cv2.imwrite('/media/dcofer/Ubuntu_Data/drone_images/foreground_roi.png', foreground_roi) # Put them together merged_roi = cv2.add(background_roi, foreground_roi) # showAndWait('merged_roi', merged_roi) # cv2.imwrite('/home/mfp/drone-net/test/merged_roi.png', merged_roi) # Now randomly add blur blur_rand = np.random.randint(0, 100) if blur_rand < self.blur_thresh: logging.info( " blur_rand: {}. bluring image.".format(blur_rand)) blur_val = np.random.randint(1, self.blur_max) logging.info(" blur_val: {}".format(blur_val)) if blur_val > 0: # blured_roi = cv2.GaussianBlur(merged_roi, (blur_val, blur_val), 0) blured_roi = cv2.blur(merged_roi, (blur_val, blur_val)) else: blured_roi = merged_roi # cv2.imwrite('/home/mfp/drone-net/test/blured_roi.png', blured_roi) else: logging.info( " blur_rand: {}. Leaving image un-blurred".format( blur_rand)) blured_roi = merged_roi # Now put them back into the canvas canvas_img[paste_y:(paste_y + paste_height), paste_x:(paste_x + paste_width)] = blured_roi # utils.showAndWait('canvas_img', canvas_img) # Now put the label into the canvas ret, label_mask = cv2.threshold(rotated_paste_mask, 5, 255, cv2.THRESH_BINARY) where_label = np.array(np.where(label_mask)) canvas_label_roi = canvas_label[paste_y:(paste_y + label_mask.shape[0]), paste_x:(paste_x + label_mask.shape[1]), 2] canvas_label_roi[where_label[0], where_label[1]] = 255 canvas_label[paste_y:(paste_y + label_mask.shape[0]), paste_x:(paste_x + label_mask.shape[1]), 2] = canvas_label_roi # utils.showAndWait('canvas_label', canvas_label) self.canvas_paste_links.append([ canvas_idx, tile_idx, canvas_img_file, paste_img_file, paste_x, paste_y, paste_width, paste_height ]) json_label = { "class": "rect", "height": paste_height, "width": paste_width, "x": paste_x, "y": paste_y } labels.append(json_label) self.incrementNextPastedImageIndex(paste_img_files) # canvas_img = utils.drawLabels(canvas_img, labels) # utils.showAndWait('canvas_img', canvas_img) save_img_file = save_img_dir + '/{}_{}_{}.jpg'.format( self.file_prefix, canvas_idx, tile_idx) cv2.imwrite(save_img_file, canvas_img) logging.info("saving image: {}".format(save_img_file)) #misc.imsave(save_file, canvas_img) save_label_file = save_label_dir + '/{}_{}_{}.txt'.format( self.file_prefix, canvas_idx, tile_idx) utils.saveYoloLabelFile(0, labels, save_label_file, canvas_width, canvas_height) logging.info("saving lable: {}".format(save_label_file)) # # save_label_file = save_label_dir + '/()_{}_{}_label.png'.format(self.file_prefix, canvas_idx, tile_idx) # cv2.imwrite(save_label_file, canvas_label) # logging.info("saving lable image: {}".format(save_label_file)) json_label = { "class": "image", "filename": save_img_file, "annotations": labels } out_labels.append(json_label)