def saveFrame(self, frame=None, default_name=True, directory=None): """ Write the input frame to Camera/Images :param frame: A numpy array containing the frame to write (shape = (height, width, 3)) :return: None """ if not default_name: name = time.strftime("%c").replace(":", ";") + ".png" print("Frame saved as " + str(name)) print("in directory:\n" + config.get_dir(":images")) else: name = datetime.datetime.now().strftime( "%a %b %d %H;%M;%S.%f %p, %Y") + ".png" if frame is None: frame = self.frame if directory == None: directory = config.get_dir(":images") if not os.path.isdir(directory): os.makedirs(directory) cv2.imwrite(directory + name, frame)
def initVideoWriter(self, fps=30, video_name=None, includeTimestamp=True, codec='mp4v', format='mov', output_dir=None): """ Initialize the Capture's video writer. :param fps: The playback FPS of the video. This number can be finicky as the capture's current FPS may not match the video's output FPS. This is because video playback takes less computation than analyzing the video in this setting. :param video_name: The name of the video. If "", a time stamp will automatically be inserted :param includeTimestamp: An optional parameter specifying whether the time should be included. True by default :param codec: The output video codec. mp4v is recommended :return: None """ if video_name == None: video_name = "" elif video_name != None and includeTimestamp == True: video_name += " " if includeTimestamp == True: video_name += time.strftime("%c").replace(":", ";") + "." + format if output_dir == None: output_dir = config.get_dir(":videos") + video_name else: output_dir += "/" + video_name if not os.path.isdir(output_dir): os.makedirs(output_dir) fourcc = cv2.VideoWriter_fourcc(*codec) self.video = cv2.VideoWriter() self.video.open(output_dir, fourcc, fps, (int(self.dimensions[2] - self.dimensions[0]), int(self.dimensions[3] - self.dimensions[1])), True) self.videoOutputDir = output_dir print("Initialized video named '%s'." % (video_name))
def loadVideo(self, camSource): print("loading video into window named '" + str( self.windowName) + "'...") self.camera = cv2.VideoCapture(config.get_dir(":videos") + camSource) self.cameraFPS = self.camera.get(cv2.CAP_PROP_FPS) self.lenVideoFrames = int(self.camera.get(cv2.CAP_PROP_FRAME_COUNT)) if self.lenVideoFrames <= 0: raise Exception( "Video failed to load! Did you misspell the video name?") self.videoLength_sec = self.lenVideoFrames / self.cameraFPS self.singleFrame_sec = 1.0 / (self.cameraFPS * 1000) print("\tfps:", self.cameraFPS) print("\tlength (sec):", self.videoLength_sec) print("\tlength (frames):", self.lenVideoFrames) cv2.createTrackbar(self.trackbarName, self.windowName, 0, # int(self.camera.get(cv2.CAP_PROP_FRAME_COUNT)), self.lenVideoFrames, self.onSlider) print("video loaded!")
def convert_gpx(directory): directory = config.get_dir(directory) with open(directory, 'r') as gpx_file: contents = gpx_file.read() data = [] while len(contents) > 2: lat_index_start = contents.find("lat") + 5 lat_index_end = contents.find('"', lat_index_start) latitude = contents[lat_index_start: lat_index_end] contents = contents[lat_index_end:] lon_index_start = contents.find("lon") + 5 lon_index_end = contents.find('"', lon_index_start) longitude = contents[lon_index_start: lon_index_end] data.append([latitude, longitude]) contents = contents[lon_index_end:] data.pop(-1) map.write_map(data, directory[:-4])
def run(): paused = True jump_dist = 1 write_negatives = True image_shape = None global current_image print("write_negatives is " + str(write_negatives)) if paused == True: camera1.getFrame(readNextFrame=False) camera1.showFrame() cv2.setMouseCallback(camera1.windowName, on_mouse) while camera1.isRunning: if paused == False: camera1.getFrame() camera1.showFrame() if write_negatives == True: if image_shape is not None: frame = cv2.resize(camera1.frame, image_shape) else: frame = camera1.frame camera1.saveFrame(frame, directory=config.get_dir( ":images") + "negatives/") key = camera1.getPressedKey() if key == 'q' or key == "esc": camera1.stopCamera() break elif key == ' ': if paused: print("...Video unpaused") else: print("Video paused...") update_rect() paused = not paused elif key == '.': if jump_dist == 1: jump_dist = 10 print("Long jump for selection rectangle set") else: jump_dist = 1 print("Short jump for selection rectangle set") if key == "1": current_image = 1 print("current image is", current_image) elif key == "2": current_image = 2 print("current image is", current_image) elif key == "3": current_image = 3 print("current image is", current_image) elif key == "4": current_image = 4 print("current image is", current_image) if paused: if key == 'enter': cropped = camera1.frame[selection[1]: selection[3], selection[0]: selection[2]] if image_shape is not None: cropped = cv2.resize(cropped, image_shape) print("current image is", current_image) if current_image == 1: print("1") camera1.saveFrame(cropped, directory=config.get_dir( ":images") + "positives1/") elif current_image == 2: print("2") camera1.saveFrame(cropped, directory=config.get_dir( ":images") + "positives2/") elif current_image == 3: print("3") camera1.saveFrame(cropped, directory=config.get_dir( ":images") + "positives3/") elif current_image == 4: print("4 ") camera1.saveFrame(cropped, directory=config.get_dir( ":images") + "positives4/") #camera1.saveFrame(cropped, # directory=config.get_dir(":images") + "positives/") camera1.incrementFrame() camera1.getFrame() update_rect() print("Positive frame saved!") elif key == 'n': write_negatives = not write_negatives print("write_negatives is " + str(write_negatives)) elif key == 'g': selection[0] -= jump_dist selection[2] += jump_dist selection[1] -= jump_dist selection[3] += jump_dist if selection[0] < 0: selection[0] = 0 if selection[2] >= camera1.frame.shape[1]: selection[2] = camera1.frame.shape[1] - 1 if selection[1] < 0: selection[1] = 0 if selection[3] >= camera1.frame.shape[0]: selection[3] = camera1.frame.shape[0] - 1 update_rect() elif key == 's': selection[0] += jump_dist selection[2] -= jump_dist selection[1] += jump_dist selection[3] -= jump_dist if selection[0] > selection[2]: selection[0] = selection[2] - 1 if selection[1] > selection[3]: selection[1] = selection[3] - 1 update_rect() elif key == 'down': selection[1] += jump_dist selection[3] += jump_dist update_rect() elif key == 'up': selection[1] -= jump_dist selection[3] -= jump_dist update_rect() elif key == 'right': if selection == None: camera1.getFrame() camera1.showFrame() else: selection[0] += jump_dist selection[2] += jump_dist update_rect() elif key == 'left': if selection == None: camera1.decrementFrame() camera1.getFrame(readNextFrame=False) camera1.showFrame() else: selection[0] -= jump_dist selection[2] -= jump_dist update_rect()
return index + 1 return False def is_near(self, index, position): dist_lat = abs(float(self.map[index][0]) - position[0]) dist_lng = abs(float(self.map[index][1]) - position[1]) dist = (dist_lat ** 2) + (dist_lng ** 2) ** (0.5) if dist > self.accuracy: return False elif dist <= self.accuracy: return True if __name__ == "__main__": import sys sys.path.insert(0, "../") import config map = get_map(config.get_dir(":maps") + "2015-11-08 07_58_30.csv") binder = Binder(map) pos = binder.bind((40.44051147069823, -79.94248582057649)) pre_bind = binder.prevBind print((binder.map[pre_bind])) print((pos, pre_bind))
def write_from_images(input_dir, output_dir, fps, video_name, add_timestamp, format, width, height): if input_dir == None: input_dir = config.get_dir(":images") if output_dir == None: output_dir = config.get_dir(":videos") if fps == None: fps = 5 if add_timestamp == None: add_timestamp = True files = natural_sort(os.listdir(input_dir)) images = [] print("Using files:") for file in files: image = cv2.imread(input_dir + "/" + file) if image != None: print(file) if width is None and height is None: height, width = image.shape[0:2] else: image = cv2.resize(image, (width, height)) images.append(image) print("Number of frames:", len(images)) if len(images) == 0: print("No images found!") quit() if format == None: format = 'avi' if format != "mov" and format != "m4v": codec = "MJPG" else: codec = "mp4v" if video_name == None: video_name = "" elif video_name != None and add_timestamp == True: video_name += " " if video_name == None or add_timestamp == True: video_name += time.strftime("%c").replace(":", ";") + "." + format fourcc = cv2.VideoWriter_fourcc(*codec) video = cv2.VideoWriter() video.open(output_dir + video_name, fourcc, fps, (width, height), True) print("Initialized video named '%s'.\nIt will be written to:\n%s" % ( video_name, output_dir)) for image in images: video.write(image) video.release() print("Success!!!")
dbcur.execute(sql) dbcur.close() #logging.basicConfig(filename= '../logs/insert_mysql.log',level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)s %(message)s') if __name__ == '__main__': logging.info("Using the following configuration: %s", str(configure_db())) mysql_conn = configure_db() x = mysql_conn.cursor() try: input_file = sys.argv[1] except: input_file = config.get_dir(1) + "/data/test.csv" logging.info("Using %s as input file",input_file) f = open(input_file,"r") content = f.readlines() counter = 0 correct_count = 0 for l in content: fields = l.split(";") logging.debug("Inserting row [len=%d]..." % len(fields)) if len(fields) == 8: logging.debug("(%s|%s|%s|%s|%s|%s|%s|1)" % (fields[0],fields[1],fields[2],fields[3],fields[4],fields[5],fields[6])) try: