def ChangeBackg2Gray(PathInput, Pathoutput): """ Get a video and grayscale the background leaving the objects in colors :param PathInput: string. Path input :param Pathoutput: string. Path output :return: Nothing """ cap = cv2.VideoCapture(PathInput) fps = int(cap.get(cv2.CAP_PROP_FPS)) change_bg = alter_bg(model_type="pb") change_bg.load_pascalvoc_model("xception_pascalvoc.pb") change_bg.gray_video(PathInput, frames_per_second=fps, output_video_name=Pathoutput, detect='person')
def ChangeBackground(BackgroundImage, PathInput, Pathoutput): """ Change the background of a video to a specific image. The objects in the frames do not change :param BackgroundImage: np.ndarray . Image to set as the background :param PathInput: string. Path input :param Pathoutput: string. Path output :return: Nothing """ cap = cv2.VideoCapture(PathInput) fps = int(cap.get(cv2.CAP_PROP_FPS)) change_bg = alter_bg(model_type="pb") change_bg.load_pascalvoc_model("xception_pascalvoc.pb") change_bg.change_video_bg(PathInput, BackgroundImage, frames_per_second=fps, output_video_name=Pathoutput, detect='person')
# lets begin. # first , lets download the model : # model link : # https://github.com/ayoolaolafenwa/PixelLib/releases/download/1.1/xception_pascalvoc.pb # copy the file to c:/models import pixellib from pixellib.tune_bg import alter_bg import cv2 cam = cv2.VideoCapture(0) # connet to camera # set the captue resolution cam.set(3,1920) cam.set(4,1080) change_bg = alter_bg(model_type="pb") change_bg.load_pascalvoc_model('c:/models/xception_pascalvoc.pb') # press q to exit the camera seg , result = change_bg.color_camera(cam, frames_per_second=10, colors = (200,129,160), show_frames=True, frame_name="frame", detect="person", output_video_name="c:/demo/cameraSolidColor.mp4") # in this sample we change the background color to white : 255,255,255 and we save it to a file : "c:/demo/cameraSolidColor.mp4" #lets change to another color
def create_xml_from_image(self, output_loc, classification_name): """Method to extract largest identified object in image using FasterRCNN. Method provides both a photo of image with a bounding box and a associate XML PASCAL VOC file for every image that contains the bounding box information. Args: output_loc: Folder containing all the images Returns: None """ for filename in os.listdir(output_loc): if filename.endswith(".jpg") or filename.endswith(".png"): #Use FasterRCNN to find the drone and segment it to a white background change_bg = alter_bg(model_type="pb") change_bg.load_pascalvoc_model("xception_pascalvoc.pb") print(output_loc + filename) change_bg.color_bg(output_loc + '/' + filename, colors=(255, 255, 255), output_image_name=output_loc + '/boxed/boxed' + filename) #Read in segmented image frame = cv2.imread(output_loc + '/boxed/boxed' + filename) # Our operations on the frame come here gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (7, 7), 0) gray = cv2.medianBlur(gray, 3) #to remove salt and paper noise #Binary threshold ret, thresh = cv2.threshold(gray, 200, 255, 0) #Outer boundery only kernel = np.ones((2, 2), np.uint8) thresh = cv2.morphologyEx(thresh, cv2.MORPH_GRADIENT, kernel) #to strength week pixels thresh = cv2.dilate(thresh, kernel, iterations=5) contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) #Draw bounding box cv2.drawContours(frame, contours, -1, (0, 255, 0), 5) # find the biggest countour (c) by the area c = max(contours, key=cv2.contourArea) if contours else None x, y, w, h = cv2.boundingRect(c) # draw the biggest contour (c) in blue cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) # Write resulting frame cv2.imwrite(output_loc + '/boxed/boxed' + filename, frame) #Variables xmin = x xmax = x + w ymin = y ymax = y + h width, height, depth = frame.shape #Create new XML file in PASCAL VOC format with open('box_output.xml', encoding='latin-1') as f: tree = ET.parse(f) root = tree.getroot() #Replace values in XML file for elem in root.getiterator(): try: elem.text = elem.text.replace( 'foldername_', output_loc) elem.text = elem.text.replace( 'filename_', filename) elem.text = elem.text.replace('width_', str(width)) elem.text = elem.text.replace( 'height_', str(height)) elem.text = elem.text.replace('depth_', str(depth)) elem.text = elem.text.replace( 'Drone_name', str(classification_name)) elem.text = elem.text.replace('xmin_', str(xmin)) elem.text = elem.text.replace('xmax_', str(xmax)) elem.text = elem.text.replace('ymin_', str(ymin)) elem.text = elem.text.replace('ymax_', str(ymax)) except AttributeError: pass #Output new XML file tree.write(output_loc + '/' + filename[:-4] + '.xml', encoding='latin-1') else: continue #Allow user to scan through images in the 'boxed' for mistakes and have those files erased while True: if input( "Please take this time to scan through 'images/boxed' folder for images where the bounding box is not correctly identifying the drone. Please delete all misclassified images. Once that is complete please press Enter" ) != 'y': break #Remove corresponding jpg/xml files that users deleted from the bounding box images folder boxed_files = [str(x)[5:-3] for x in os.listdir(output_loc + '/boxed')] print(boxed_files) for filename in os.listdir(output_loc): if filename.endswith(".xml"): if filename[:-3] not in boxed_files: try: os.remove(output_loc + '/' + filename) except: continue #Remove jpg files where bounding boxes were not present list_files = [str(x)[:-3] for x in os.listdir(output_loc)] single_files = [i for i in list_files if list_files.count(i) == 1] for file in single_files: try: os.remove(output_loc + '/' + file + 'jpg') except: continue #Break the images and XML files into train, test, and valid folders jpg_count = 1 for filename in os.listdir(output_loc): if filename.endswith(".jpg") or filename.endswith(".png"): if jpg_count % 2 == 0: shutil.move(output_loc + '/' + str(filename), output_loc + '/train/' + str(filename)) shutil.move( output_loc + '/' + str(filename)[:-3] + 'xml', output_loc + '/train/' + str(filename)[:-3] + 'xml') jpg_count += 1 elif str(jpg_count).endswith('1') or str(jpg_count).endswith( '3') or str(jpg_count).endswith('5'): shutil.move(output_loc + '/' + str(filename), output_loc + '/valid/' + str(filename)) shutil.move( output_loc + '/' + str(filename)[:-3] + 'xml', output_loc + '/valid/' + str(filename)[:-3] + 'xml') jpg_count += 1 else: shutil.move(output_loc + '/' + str(filename), output_loc + '/test/' + str(filename)) shutil.move( output_loc + '/' + str(filename)[:-3] + 'xml', output_loc + '/test/' + str(filename)[:-3] + 'xml') jpg_count += 1
fgbg = cv2.createBackgroundSubtractorMOG2() while (1): ret, frame = cap.read() fgmask = fgbg.apply(frame) cv2.imshow('Input', frame) cv2.imshow('fgmask', fgmask) k = cv2.waitKey(60) & 0xff if k == ord("q"): break cap.release() cv2.destroyAllWindows() change_bg = alter_bg(model_type="pb") #loading_data_model change_bg.load_pascalvoc_model("xception_pascalvoc.pb") #loading_data_model def bgblur(): capture = cv2.VideoCapture(0) change_bg.blur_camera(capture, frames_per_second=10, extreme=True, show_frames=True, frame_name="frame", output_video_name="bgblur_out.mp4") def bgcolor(): capture = cv2.VideoCapture(0)
from pixellib.tune_bg import alter_bg change_bg = alter_bg() print(change_bg) change_bg.load_pascalvoc_model( "deeplabv3_xception_tf_dim_ordering_tf_kernels.h5") print("-=-=-") change_bg.change_bg_img( f_image_path="img/sample.jpg", b_image_path="img/background.jpg", output_image_name="img/new_img.jpg", ) print("opopop")
import pixellib from pixellib.tune_bg import alter_bg effects_obj = alter_bg(model_type='pb') effects_obj.load_pascalvoc_model('xception_pascalvoc.pb') effects_obj.blur_bg("bedroom.png", extreme=True, output_image_name="outty.jpg")