class CustomStream: def __init__(self, src=0, use_cv2=False): if use_cv2: self.obj = cv2.VideoCapture(src) elif src == 0: self.obj = WebcamVideoStream(src) elif src != 0: self.obj = FileVideoStream(src) def isOpened(self): if isinstance(self.obj, cv2.VideoCapture): return self.obj.isOpened() return self.obj.stream.isOpened() def start(self): self.obj.start() return self def update(self): self.obj.update() def read(self): if isinstance(self.obj, cv2.VideoCapture): return self.obj.read() return not self.obj.stopped, self.obj.read() def stop(self): self.obj.stop() if isinstance(self.obj, cv2.VideoCapture): self.obj.release() else: self.obj.stream.release() def set(self, propId, value): if isinstance(self.obj, cv2.VideoCapture): self.obj.set(propId, value) return self.obj.stream.set(propId, value) def get(self, propId): if isinstance(self.obj, cv2.VideoCapture): self.obj.get(propId) return return self.obj.stream.get(propId)
def fry_gif(update, url, number_of_cycles, args): number_of_emojis = 1.5 if args['high-fat'] else 1 if args['low-fat'] else 0 bulge_probability = 0.3 if args['heavy'] else 0.15 if args['light'] else 0 magnitude = 4 if args['deep'] else 1 if args['shallow'] else 2 name = update.message.from_user.first_name filename = '%s_%s_%s' % (update.message.chat_id, name, update.message.message_id) filepath = bin_path + '/temp/' + filename caption = __get_caption(name, number_of_cycles, args) output = bin_path + '/temp/out_' + filename + '.mp4' gifbio = BytesIO() gifbio.name = filename + '.gif' fs = [__posterize, __sharpen, __increase_contrast, __colorize] shuffle(fs) if not __download_gif(url, filepath): return fvs = FileVideoStream(filepath + '.mp4').start() frame = fvs.read() height, width, _ = frame.shape try: fps = fvs.get(CAP_PROP_FPS) except: fps = 30 out = VideoWriter(output, VideoWriter_fourcc(*'mp4v'), fps, (width, height)) out.write( fry_frame(frame, number_of_cycles, fs, number_of_emojis, bulge_probability, magnitude, args)) while fvs.more() or fvs.more(): try: temp = fry_frame(fvs.read(), number_of_cycles, fs, number_of_emojis, bulge_probability, magnitude, args) except Exception as e: break out.write(temp) fvs.stop() fvs.stream.release() out.release() update.message.reply_animation(open(output, 'rb'), caption=caption, quote=True) try: __upload_to_imgur(output, caption) except (Exception, BaseException) as e: print(e) remove(filepath + '.mp4')
def fry_gif(update, url, number_of_cycles, args): log_debug('Starting GIF Fry') number_of_emojis = (1.5 if args['high-fat'] else 1 if args['low-fat'] else 0) bulge_probability = (0.3 if args['heavy'] else 0.15 if args['light'] else 0) magnitude = (4 if args['deep'] else 1 if args['shallow'] else 2) name = update.message.from_user.first_name filename = '%s_%s_%s' % (update.message.chat_id, name, update.message.message_id) filepath = f'{bin_path}/temp/{filename}' caption = __get_caption(name, number_of_cycles, args) output = f'{bin_path}/temp/out_{filename}.mp4' gif_bio = BytesIO() gif_bio.name = f'{filename}.gif' if not __download_gif(url, filepath): log_error('GIF download failed') return log_debug('GIF successfully downloaded') fvs = FileVideoStream(f'{filepath}.mp4').start() frame = fvs.read() height, width, _ = frame.shape try: fps = fvs.get(CAP_PROP_FPS) log_debug(f'Detected FPS: {fps}') except: log_warn('FPS Detection failed, defaulting to 30.') fps = 30 out = VideoWriter(output, VideoWriter_fourcc(*'mp4v'), fps, (width, height)) fs = [__posterize, __sharpen, __increase_contrast, __colorize] shuffle(fs) log_debug(f'Frying first frame') out.write( fry_frame(frame, number_of_cycles, fs, number_of_emojis, bulge_probability, magnitude, args)) i = 2 while fvs.more() or fvs.more(): try: log_debug(f'Frying frame {i}') temp = fry_frame(fvs.read(), number_of_cycles, fs, number_of_emojis, bulge_probability, magnitude, args) out.write(temp) log_debug(f'Frame {i} fried successfully') i += 1 except Exception: log_error(f'Encountered error while frying frame {i}') break log_debug(f'All frames fried.') fvs.stop() fvs.stream.release() out.release() update.message.reply_animation(open(output, 'rb'), caption=caption, quote=True) log_debug(f'GIF saved and replied') try: __upload_to_imgur(output, caption) except (Exception, BaseException) as e: print(e) try: remove(f'{filepath}.mp4') except: pass log_debug('Image frying process completed')
print("[INFO] loading YOLO from disk...") net = cv2.dnn.readNetFromDarknet(configPath, weightsPath) ln = net.getLayerNames() ln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()] # initialize the video stream, pointer to output video file, and # frame dimensions #fvs = cv2.VideoCapture(args["input"]) writer = None (W, H) = (None, None) # try to determine the total number of frames in the video file try: prop = cv2.cv.CV_CAP_PROP_FRAME_COUNT if imutils.is_cv2() \ else cv2.CAP_PROP_FRAME_COUNT total = int(fvs.get(prop)) print("[INFO] {} total frames in video".format(total)) # an error occurred while trying to determine the total # number of frames in the video file except: print("[INFO] could not determine # of frames in video") print("[INFO] no approx. completion time can be provided") total = -1 # loop over frames from the video file stream while fvs.more(): # read the next frame from the file frame = fvs.read() # if the frame was not grabbed, then we have reached the end