def run(): cap = cv2.VideoCapture(0) save_path = 'saved-media/glasses_and_stash.mp4' frames_per_seconds = 24 config = CFEVideoConf(cap, filepath=save_path, res='720p') out = cv2.VideoWriter(save_path, config.video_type, frames_per_seconds, config.dims) face_cascade = cv2.CascadeClassifier( 'cascades/data/haarcascade_frontalface_default.xml') eyes_cascade = cv2.CascadeClassifier( 'cascades/third-party/frontalEyes35x16.xml') nose_cascade = cv2.CascadeClassifier('cascades/third-party/Nose18x15.xml') glasses = cv2.imread("images/glasses/11.png", -1) mustache = cv2.imread("images/mustache/mustache.png", -1) imgHair = cv2.imread('images/hair/2.png', -1) while (True): # Capture frame-by-frame ret, frame = cap.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, scaleFactor=1.5, minNeighbors=5) frame = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA) for (x, y, w, h) in faces: y = y - 70 roi_gray = gray[y:y + h, x:x + h] # rec roi_color = frame[y:y + h, x:x + h] #cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 255, 255), 3) eyes = eyes_cascade.detectMultiScale(roi_gray, scaleFactor=1.5, minNeighbors=5) for (ex, ey, ew, eh) in eyes: #cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 3) roi_eyes = roi_gray[ey:ey + eh, ex:ex + ew] glasses2 = image_resize(glasses.copy(), width=ew) gw, gh, gc = glasses2.shape for i in range(0, gw): for j in range(0, gh): #print(glasses[i, j]) #RGBA if glasses2[i, j][3] != 0: # alpha 0 roi_color[ey + i, ex + j] = glasses2[i, j] nose = nose_cascade.detectMultiScale(roi_gray, scaleFactor=1.5, minNeighbors=5) for (nx, ny, nw, nh) in nose: #cv2.rectangle(roi_color, (nx, ny), (nx + nw, ny + nh), (255, 0, 0), 3) roi_nose = roi_gray[ny:ny + nh, nx:nx + nw] mustache2 = image_resize(mustache.copy(), width=nw) mw, mh, mc = mustache2.shape for i in range(0, mw): for j in range(0, mh): #print(glasses[i, j]) #RGBA if mustache2[i, j][3] != 0: # alpha 0 roi_color[ny + int(nh / 3.0) + i, nx + j] = mustache2[i, j] face = face_cascade.detectMultiScale(roi_gray, scaleFactor=1.5, minNeighbors=5) for (fx, fy, fw, fh) in face: fx = fx - 40 fy = fy - 120 #cv2.rectangle(roi_color, (fx, fy), (fx + fw, fy + fh), (255, 0, 0), 3) roi_face = roi_gray[fy:fy + fh, fx:fx + fw] imgHair2 = image_resize(imgHair.copy(), width=fw + int(fw / 6)) hw, hh, hc = imgHair2.shape for i in range(0, hw): for j in range(0, hh): #print(glasses[i, j]) #RGBA if imgHair2[i, j][3] != 0: # alpha 0 roi_color[fy + i, fx + j] = imgHair2[i, j] # Display the resulting frame frame = cv2.cvtColor(frame, cv2.COLOR_BGRA2BGR) out.write(frame) cv2.imshow('frame', frame) if cv2.waitKey(20) & 0xFF == ord('q'): break # When everything done, release the capture cap.release() out.release() cv2.destroyAllWindows()
import numpy as np import cv2 from utils import CFEVideoConf, image_resize cap = cv2.VideoCapture(0) save_path = 'saved-media/watermark.mp4' frames_per_seconds = 24 config = CFEVideoConf(cap, filepath=save_path, res='720p') out = cv2.VideoWriter(save_path, config.video_type, frames_per_seconds, config.dims) img_path = 'images/logo/cfe-coffee.png' logo = cv2.imread(img_path, -1) watermark = image_resize(logo, height=50) #watermark = cv2.cvtColor(watermark, cv2.COLOR_BGR2GRAY) #watermark = cv2.cvtColor(watermark, cv2.COLOR_GRAY2BGR) watermark = cv2.cvtColor(watermark, cv2.COLOR_BGR2BGRA) # grayscale watermark # cv2.imshow('watermark', watermark) #print(watermark.shape) while (True): # Capture frame-by-frame ret, frame = cap.read() frame = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA) # print(frame[50, 150]) # numpy array # start_cord_x = 50 # start_cord_y = 150
in_vid = cv2.VideoCapture('example.mp4') img_dir = "Frames" seconds_duration = 16 seconds_between_shots = .35 try: if not os.path.exists(img_dir): os.makedirs(img_dir) except OSError: print('Error: Creating directory of frames') now = datetime.datetime.now() finish_time = now + datetime.timedelta(seconds=seconds_duration) currentFrame = 0 frame_rate = 40 save_path = 'output.mp4' config = CFEVideoConf(in_vid, filepath=save_path, res='720p') out = cv2.VideoWriter(save_path, config.video_type, frame_rate, config.dims) while datetime.datetime.now() < finish_time: ret, frame = in_vid.read() filename = f"{img_dir}/{currentFrame}.jpg" currentFrame += 1 cv2.imwrite(filename, frame) time.sleep(seconds_between_shots) # Combine images to Video image_list = glob.glob(f"{img_dir}/*.jpg") for file in image_list: image_frame = cv2.imread(file) image = image_resize(image_frame, width=config.width) out.write(image)
import numpy as np import cv2 from utils import CFEVideoConf, image_resize filename = 'watermark.mp4' frames_per_seconds = 24.0 cap = cv2.VideoCapture(0) config = CFEVideoConf(cap, filepath=filename, res='720p') out = cv2.VideoWriter(filename, config.video_type, frames_per_seconds, config.dims) watermark = 'Marketing-Communications-Logo-1.png' image = cv2.imread(watermark, -1) image = image_resize(image, 100, 100) cv2.imshow('watermark', image) while True: ret, frame = cap.read() out.write(frame) cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() out.release() cv2.destroyAllWindows()
from utils import CFEVideoConf, image_resize import glob import math import cv2 import numpy as np cap = cv2.VideoCapture(0) framesPerSecond = 24.0 savePath = 'filter.mp4' config = CFEVideoConf(cap, filepath=savePath, res='480p') # Filters def applyInvert(frame): return cv2.bitwise_not(frame) def verifyAlphaChannel(frame): try: frame.shape[3] except IndexError: frame = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA) return frame def applyColor(frame, intensity=0.2, blue=0, green=0, red=0): frame = verifyAlphaChannel(frame) frameH, frameW, frameC = frame.shape sepiaBGRA = (blue, green, red, 1) overlay = np.full((frameH, frameW, 4), sepiaBGRA, dtype='uint8')
import numpy as np import os import cv2 #WEBCAM MAX RES 640 x 480 from utils import CFEVideoConf, image_resize cap = cv2.VideoCapture(0) save_path = 'saved-media/watermark.mp4' frames_per_second = 24 config = CFEVideoConf(cap, filepath=save_path, res='480') out = cv2.VideoWriter(save_path, config.video_type, frames_per_second, config.dims) img_path = 'logo.jpg' logo = cv2.imread(img_path, -1) watermark = image_resize(logo, height=50) cv2.imshow('watermark', watermark) while (True): # capture frame by frame ret, frame = cap.read() # out.write(frame) # display the frame cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release()
import numpy as np import cv2 from utils import CFEVideoConf, image_resize cap = cv2.VideoCapture(0) save_path = 'g:/videos/watermark.mp4' frames_per_seconds = 24 config = CFEVideoConf(cap, filepath=save_path, res='480p') # used to change the resolution out = cv2.VideoWriter(save_path, config.video_type, frames_per_seconds, config.dims) img_path = 'G:\pictures/cats.png' logo = cv2.imread(img_path,-1) #watermark = cv2.cvtColor(watermark, cv2.COLOR_BGR2BGRA) watermark = image_resize(logo, height = 100) # gray scale, do not have number 3 watermark = cv2.cvtColor(watermark, cv2.COLOR_BGR2BGRA) #has number 3 #cv2.imshow('watermark', watermark) while(True): # Capture frame-by-frame ret, frame = cap.read() frame = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA) # used to demonstrate # start_cord_x = 50 # start_cord_y = 150 # stroke = 2 # color = [255,255,0] # w = 100 # h = 200