diff --git a/Tkinter/GUI.py b/Tkinter/GUI.py index 0d5e101..ede888d 100644 --- a/Tkinter/GUI.py +++ b/Tkinter/GUI.py @@ -7,90 +7,39 @@ from PIL import Image, ImageTk import time import cv2 +from Tkinter.GUI_Live import camera # from snapshot import App, videoCapture root = tk.Tk() images = [] +from Tkinter.GUI_Image import importWindowyahia ######### import Window ########## def importWindow(): - newWindow = Toplevel(root) - newWindow.geometry("1000x1000") - def browse(): - - filename = filedialog.askopenfilename(initialdir="/home/yousef/Pictures", title="select File", - filetypes = (("jpeg files","*.jpg"),("all files","*.*"),('png files', '*.png'))) - - images.append(filename) - canvas = Canvas(newWindow) - canvas.config(width=1000, height=500) - canvas.grid(row=1 , column=0,columnspan=3) - print(filename) - img = ImageTk.PhotoImage(Image.open(images[0])) - canvas.create_image(0, 0, anchor="nw", image=img) - mainloop() - - root.withdraw() - newWindow.title("New Window") - newWindow.geometry("700x700") - backButton = tk.Button(newWindow, text="Browse", padx=90, pady=10, fg="white", bg="#2596be", font=('arial', 15),command=browse) - backButton.grid(row=0 , column=0) - - ######## Back to previous window ######## - def on_closing(): - newWindow.destroy() - root.deiconify() - backButton = tk.Button(newWindow, text="Previous Page", padx=90, pady=10, fg="white", bg="#2596be", font=('arial', 15), command=on_closing) - backButton.grid(row=0 , column=2) - - newWindow.protocol("WM_DELETE_WINDOW", on_closing) - - + importWindowyahia(root) ########### second-window ############# def videoWindow(): - def openCamera(): - # cap=cv2.VideoCapture(0) - # frame=cap.read() - # cv2.imshow('frame', frame) - # cv2.waitKey(0) - cap = cv2.VideoCapture(0) - while True: - _, frame = cap.read() - cv2.imshow('frame', frame) - cv2.waitKey(1) - - - canv = Canvas(newWindow, width=150, height=150, bg='white') - canv.grid(row=2, column=3) - - - img = ImageTk.PhotoImage(Image.open('123.png')) - canv.create_image(20, 20, anchor=NW, image=img) - - - newWindow = Toplevel(root) + cameraWindow = Toplevel(root) root.withdraw() - newWindow.title("New Window") - newWindow.geometry("700x700") - Label(newWindow, + cameraWindow.title("New Window") + cameraWindow.geometry("700x700") + Label(cameraWindow, text="This is a new window").grid() - backButton = tk.Button(newWindow, text="Browse", padx=90, pady=10, fg="white", bg="#2596be", font=('arial', 15), command=openCamera) - backButton.grid() - + camera(cameraWindow) ######## Back to previous window ######## def on_closing(): - newWindow.destroy() + cameraWindow.destroy() root.deiconify() - backButton = tk.Button(newWindow, text="Previous Page", padx=90, pady=10, fg="white", bg="#2596be", font=('arial', 15), command=on_closing) + backButton = tk.Button(cameraWindow, text="Previous Page", padx=90, pady=10, fg="white", bg="#2596be", font=('arial', 15), command=on_closing) backButton.grid() - newWindow.protocol("WM_DELETE_WINDOW", on_closing) + cameraWindow.protocol("WM_DELETE_WINDOW", on_closing) @@ -107,9 +56,4 @@ def on_closing(): ########################################### -root.mainloop() - - - - - +root.mainloop() \ No newline at end of file diff --git a/Tkinter/GUI_Image.py b/Tkinter/GUI_Image.py new file mode 100644 index 0000000..ba1a9b7 --- /dev/null +++ b/Tkinter/GUI_Image.py @@ -0,0 +1,109 @@ +import tkinter as tk +from tkinter import filedialog, Text +import os, sys, subprocess +from filters_image.change_filter import change_filter +from tkinter.ttk import * +from tkinter import * +from PIL import Image, ImageTk +from filters_image.image_filtering_face import image_filtering_face +import cv2 +# root = tk.Tk() +images = [] +count = 0 +next = False +entry = "" +######### import Window ########## +def render(image_withfilter,canvas): + image_withfilter = cv2.cvtColor(image_withfilter, cv2.COLOR_BGR2RGB) + photo = ImageTk.PhotoImage(image=Image.fromarray(image_withfilter)) + canvas.create_image(0, 0, anchor="nw", image=photo)\ + .grid(column=2, row=1, columnspan=2) +def next_fun(path,canvas): + global count + + images.append(image_filtering_face( + change_filter[count]['filter'], + path, + change_filter[count]['center'], + change_filter[count]['width'], + change_filter[count]['height'], + change_filter[count]['up'], + change_filter[count]['left'], + change_filter[count]['counte'] + )) + count += 1 + if count == len(change_filter)-1: + count = -1 + render(images[-1],canvas) + +def submit(content): + global entry + entry1 = entry.get() + imgdir = f"../saved/{entry1}.png" + cv2.imwrite(imgdir, content) + entry.delete(0, END) +def saving(content,newWindow): + global entry + sub_btn = tk.Button(newWindow, text='Submit', command=lambda: submit(content)) + entry = tk.Entry(newWindow, width=20, bg="white") + entry.grid(row=1, column=2) + sub_btn.grid(row=2, column=2) + +def image_filter(path, canvas, newWindow): + print('yahia') + images.append( + image_filtering_face(["../assest/tongue.png"], path, [57], [0.6], [1.2], [-25], [0], + [0])) + save = tk.Button(newWindow, text="save", padx=90, pady=10, fg="white", bg="#2596be", + font=('arial', 15), command=lambda: saving(images[-1],newWindow)) + save.grid(row=0, column=4) + next = tk.Button(newWindow, text="next", padx=90, pady=10, fg="white", bg="#2596be", + font=('arial', 15), command=lambda: next_fun(path, canvas)) + next.grid(row=0, column=3) + render(images[-1], canvas) +def importWindowyahia(root): + newWindow = Toplevel(root) + newWindow.geometry("1000x1000") + def browse(): + global count + + + filename = filedialog.askopenfilename(title="select File", + filetypes = (("jpeg files","*.jpg"),("all files","*.*"),('png files', '*.png'))) + images.append(filename) + canvas = Canvas(newWindow) + canvas.config(width=1000, height=500) + canvas.grid(row=1 , column=0,columnspan=3) + img = ImageTk.PhotoImage(Image.open(images[0])) + + # images[0] = cv2.resize(images[0], (200, 200)) + + filtering = tk.Button(newWindow, text="Add Filter", padx=90, pady=10, fg="white", bg="#2596be", + font=('arial', 15), command=lambda: image_filter(filename, canvas, newWindow)) + filtering.grid(row=0, column=1) + + test=canvas.create_image(0, 0, anchor="nw", image=img) + test.grid(row=1 , column=0,columnspan=3) + + + # mainloop() + + root.withdraw() + root.geometry("500x500") + newWindow.title("New Window") + newWindow.geometry("500x500") + browse_button = tk.Button(newWindow, text="Browse", padx=90, pady=10, fg="white", bg="#2596be", font=('arial', 15),command=browse) + browse_button.grid(row=0 , column=0) + + + ######## Back to previous window ######## + def on_closing(): + newWindow.destroy() + root.deiconify() + backButton = tk.Button(newWindow, text="Previous Page", padx=90, pady=10, fg="white", bg="#2596be", font=('arial', 15), command=on_closing) + backButton.grid(row=0 , column=2) + + newWindow.protocol("WM_DELETE_WINDOW", on_closing) + + + diff --git a/Tkinter/GUI_Live.py b/Tkinter/GUI_Live.py new file mode 100644 index 0000000..042ec35 --- /dev/null +++ b/Tkinter/GUI_Live.py @@ -0,0 +1,125 @@ +import tkinter as tk +from PIL import ImageTk, Image +import cv2 +from filters_live.video_filtering_face import video_filtering_face,change_filter +from tkinter import Tk +from tkinter.ttk import Frame, Label +from background_live.request_image import add_path,check_image + +save = False +count_filter = 0 +count_back = 1 +show_filter_live = False +show_background_live = False + +def camera(newWindow): + app = Frame(newWindow) + app.grid() + lmain = Label(app) + lmain.grid() + + + + def printInput(): + add_path(inputtxt2.get(1.0, "end-1c")) + + inputtxt2 = tk.Text(newWindow, + height=5, + width=20) + inputtxt2.grid(row=0, column=1) + printButton = tk.Button(newWindow, + text = "Print", + command = printInput) + printButton.grid(row=0, column=1) + + def nextback(): + global count_back,show_background_live + len_image = check_image() + if count_back == len_image: + show_background_live = False + count_back=1 + else: + show_background_live = True + count_back+=1 + def nextWindow(): + global count_filter,show_filter_live + if count_filter == len(change_filter)-1: + show_filter_live = False + count_filter= -1 + else: + show_filter_live = True + count_filter+=1 + + def saveWindow(): + global save + save =True + + + importButton = tk.Button(newWindow,text="Next background", padx=90, pady=10, fg="white", bg="#2596be", font=('arial', 15), + command=nextback) + importButton.grid(row=1, column=0) + importButton = tk.Button(newWindow,text="Next filter", padx=90, pady=10, fg="white", bg="#2596be", font=('arial', 15), + command=nextWindow) + importButton.grid(row=1, column=1) + importButton = tk.Button(newWindow,text='capture and save', padx=90, pady=10, fg="white", bg="#2596be", font=('arial', 15), + command=saveWindow) + importButton.grid(row=2, column=0) + + def video_stream3(): + + frame = video_filtering_face( + change_filter[count_filter]['filter'], + change_filter[count_filter]['center'], + change_filter[count_filter]['width'], + change_filter[count_filter]['height'], + change_filter[count_filter]['up'], + change_filter[count_filter]['left'], + f'../assest/background/back{count_back}.png', + 1,show_filter_live,show_background_live + + ) + + cv2image2 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA) + img2 = Image.fromarray(cv2image2) + imgtk2 = ImageTk.PhotoImage(image=img2) + lmain.imgtk = imgtk2 + lmain.configure(image=imgtk2) + if save: + save_image(frame) + lmain.after(1, video_stream3) + def save_image(frame): + global save + save = False + cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA) + img = Image.fromarray(cv2image) + imgtk = ImageTk.PhotoImage(image=img) + lmain.imgtk = imgtk + lmain.configure(image=imgtk) + path_name(frame) + + def print_path2(inputtxt, frame): + name = inputtxt.get(1.0, "end-1c") + print(name) + img_name = f"../saved/{name}.png" + cv2.imwrite(img_name, frame) + def path_name(frame): + + inputtxt = tk.Text(newWindow, + height=5, + width=20) + + inputtxt.grid(row=1, column=2) + + printButton = tk.Button(newWindow, + text="save image", + command=lambda: print_path2(inputtxt, frame)) + printButton.grid(row=1, column=2) + + + + + video_stream3() + +# newWindow.mainloop() + + diff --git a/Tkinter/__pycache__/GUI.cpython-38.pyc b/Tkinter/__pycache__/GUI.cpython-38.pyc deleted file mode 100644 index d06f0f5..0000000 Binary files a/Tkinter/__pycache__/GUI.cpython-38.pyc and /dev/null differ diff --git a/Tkinter/__pycache__/snapshot.cpython-38.pyc b/Tkinter/__pycache__/snapshot.cpython-38.pyc deleted file mode 100644 index 5717c96..0000000 Binary files a/Tkinter/__pycache__/snapshot.cpython-38.pyc and /dev/null differ diff --git a/Tkinter/__pycache__/snapshot.cpython-39.pyc b/Tkinter/__pycache__/snapshot.cpython-39.pyc deleted file mode 100644 index 42d1e4e..0000000 Binary files a/Tkinter/__pycache__/snapshot.cpython-39.pyc and /dev/null differ diff --git a/Tkinter/test2.py b/Tkinter/test2.py new file mode 100644 index 0000000..9c88c9c --- /dev/null +++ b/Tkinter/test2.py @@ -0,0 +1,59 @@ +import tkinter as tk +from tkinter import filedialog, Text +import os, sys, subprocess +# from tkinter import * +from tkinter.ttk import * +from tkinter import * +from PIL import Image, ImageTk +import time +import cv2 +from Tkinter.GUI_Live import camera +# from snapshot import App, videoCapture +root = tk.Tk() +images = [] +from GUI_Image import importWindowyahia + + +######### import Window ########## + +def importWindow(): + importWindowyahia(root) + +########### second-window ############# + +def videoWindow(): + cameraWindow = Toplevel(root) + root.withdraw() + cameraWindow.title("New Window") + cameraWindow.geometry("700x700") + Label(cameraWindow, + text="This is a new window").grid() + + camera(cameraWindow) + + + ######## Back to previous window ######## + def on_closing(): + cameraWindow.destroy() + root.deiconify() + backButton = tk.Button(cameraWindow, text="Previous Page", padx=90, pady=10, fg="white", bg="#2596be", font=('arial', 15), command=on_closing) + backButton.grid() + + cameraWindow.protocol("WM_DELETE_WINDOW", on_closing) + + + +########## Home-Page ########## +image= PhotoImage(file ='../assest/image.png',width=700,height=700) +Label(root, image= image, bg="black",).grid(column=0,row=0,columnspan=2) + +importButton = tk.Button(text="Import", padx=90, pady=10, fg="white", bg="#2596be", font=('arial',15), command=importWindow) +importButton.grid(row=1,column=0) + + +cameraButton= tk.Button(text="Camera", padx=90, pady=10, fg="white", bg="#2596be",font=('arial',15), command=videoWindow) +cameraButton.grid(row=1,column=1) + + +########################################### +root.mainloop() \ No newline at end of file diff --git a/__pycache__/main.cpython-39.pyc b/__pycache__/main.cpython-39.pyc deleted file mode 100644 index 1a84fe7..0000000 Binary files a/__pycache__/main.cpython-39.pyc and /dev/null differ diff --git a/assest/background/back6.png b/assest/background/back6.png new file mode 100644 index 0000000..4fc8d8b Binary files /dev/null and b/assest/background/back6.png differ diff --git a/assest/background/back7.png b/assest/background/back7.png new file mode 100644 index 0000000..1c44a35 Binary files /dev/null and b/assest/background/back7.png differ diff --git a/assest/background/back8.png b/assest/background/back8.png new file mode 100644 index 0000000..9531160 Binary files /dev/null and b/assest/background/back8.png differ diff --git a/assest/background/back9.png b/assest/background/back9.png new file mode 100644 index 0000000..32d756c Binary files /dev/null and b/assest/background/back9.png differ diff --git a/background_image/Input_image/test.png b/background_image/Input_image/test.png new file mode 100644 index 0000000..1202ab7 Binary files /dev/null and b/background_image/Input_image/test.png differ diff --git a/background_image/Input_image/test4.png b/background_image/Input_image/test4.png new file mode 100644 index 0000000..1202ab7 Binary files /dev/null and b/background_image/Input_image/test4.png differ diff --git a/background_image/Output_image/test4.png b/background_image/Output_image/test4.png new file mode 100644 index 0000000..8625337 Binary files /dev/null and b/background_image/Output_image/test4.png differ diff --git a/background_image/input_background/test4.png b/background_image/input_background/test4.png new file mode 100644 index 0000000..9e08db8 Binary files /dev/null and b/background_image/input_background/test4.png differ diff --git a/background_live/__pycache__/background_live.cpython-39.pyc b/background_live/__pycache__/background_live.cpython-39.pyc deleted file mode 100644 index 4c5d07d..0000000 Binary files a/background_live/__pycache__/background_live.cpython-39.pyc and /dev/null differ diff --git a/background_live/__pycache__/request_image.cpython-39.pyc b/background_live/__pycache__/request_image.cpython-39.pyc deleted file mode 100644 index 4d607f7..0000000 Binary files a/background_live/__pycache__/request_image.cpython-39.pyc and /dev/null differ diff --git a/background_live/background_live.py b/background_live/background_live.py index fd307ec..6af1e0e 100644 --- a/background_live/background_live.py +++ b/background_live/background_live.py @@ -1,61 +1,61 @@ import cv2 import mediapipe as mp import numpy as np -from request_image import add_path -def background(path,blur =1,img=2): - mp_selfie_segmentation = mp.solutions.selfie_segmentation - back1=1 - cam = cv2.VideoCapture(0) - cam.set(3, 1280) - cam.set(4, 720) - fsize = (520, 720) +mp_selfie_segmentation = mp.solutions.selfie_segmentation +back1 = 1 +cam = cv2.VideoCapture(0) +# cam.set(3, 1280) +# cam.set(4, 720) +fsize = (520, 720) +def background(path,blur =1,img=2): - # begin with selfie segmentation model - while cam.isOpened(): - scene = cv2.imread(path) # read the scene image - scene = cv2.blur(scene, (1, 1)) - scene = cv2.resize(scene, (fsize[1], fsize[0])) # resize scene to the size of frame - with mp_selfie_segmentation.SelfieSegmentation(model_selection=1) as selfie_seg: - bg_image = scene - ret, frame = cam.read() - if not ret: - print("Error reading frame...") - continue - frame = cv2.resize(frame, (fsize[1], fsize[0])) - # flip it to look like selfie camera - frame = cv2.flip(frame, 1) - # get rgb image to pass that on selfie segmentation - rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) - # process it! - results = selfie_seg.process(rgb) + scene = cv2.imread(path) # read the scene image + # scene = cv2.blur(scene, (blur, blur)) + scene = cv2.resize(scene, (fsize[1], fsize[0])) # resize scene to the size of frame + with mp_selfie_segmentation.SelfieSegmentation(model_selection=1) as selfie_seg: + bg_image = scene + ret, frame = cam.read() + if not ret: + print("Error reading frame...") - # get the condition from result's segmentation mask - condition = np.stack((results.segmentation_mask,) * 3, axis=-1) > 0.1 + frame = cv2.resize(frame, (fsize[1], fsize[0])) - # apply background change if condition matches - output_image = np.where(condition, frame, bg_image) + # flip it to look like selfie camera + frame = cv2.flip(frame, 1) - # show the output - cv2.imshow('Background Change with MP', output_image) - key = cv2.waitKey(5) & 0xFF + # get rgb image to pass that on selfie segmentation + rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) - if key == ord('n'): - back1+=1 - if back1 ==img+1: - back1=1 - path = f'../assest/background/back{back1}.png' + # process it! + results = selfie_seg.process(rgb) + # get the condition from result's segmentation mask + condition = np.stack((results.segmentation_mask,) * 3, axis=-1) > 0.1 - if key == ord('q'): - cam.release() - cv2.destroyAllWindows() # wait until any key is pressed + # apply background change if condition matches + output_image = np.where(condition, frame, bg_image) + return output_image + # show the output + # cv2.imshow('Background Change with MP', output_image) + # key = cv2.waitKey(5) & 0xFF + # + # if key == ord('n'): + # back1+=1 + # if back1 ==img+1: + # back1=1 + # path = f'../assest/background/back{back1}.png' + # + # + # if key == ord('q'): + # cam.release() + # cv2.destroyAllWindows() # wait until any key is pressed cam.release() cv2.destroyAllWindows() diff --git a/filters_image/__pycache__/change_filter.cpython-39.pyc b/filters_image/__pycache__/change_filter.cpython-39.pyc deleted file mode 100644 index 1b3db78..0000000 Binary files a/filters_image/__pycache__/change_filter.cpython-39.pyc and /dev/null differ diff --git a/filters_image/image_filtering_face.py b/filters_image/image_filtering_face.py index 9308c74..404d7c4 100644 --- a/filters_image/image_filtering_face.py +++ b/filters_image/image_filtering_face.py @@ -2,19 +2,17 @@ import numpy as np import dlib from math import hypot -from change_filter import change_filter +# from filters_image.change_filter_image import change_filter import os -# Loading Face detector +def image_filtering_face(path_filter,path_img,center,width,height,up,left,counte=0): -def image_filtering_face(path_filter,center,width,height,up,left,counte=0): - # path = r"../assest/moustache2.png" filter_image = [] for i in path_filter: filter_image.append(cv2.imread(i)) - image = cv2.imread("saved/test.jpg") + image = cv2.imread(path_img) rows, cols, _ = image.shape filter1 = np.zeros((rows, cols), np.uint8) filter1.fill(0) @@ -24,41 +22,41 @@ def image_filtering_face(path_filter,center,width,height,up,left,counte=0): if faces: try: for i in range(len(path_filter)): - filter(image,gray_image,faces,filter_image[i],center[i],width[i],height[i],up[i],left[i]) + return filter(image,gray_image,faces,filter_image[i],center[i],width[i],height[i],up[i],left[i]) except: - image = cv2.imread("saved/test.jpg") - cv2.imshow("Frame", image) - - key = cv2.waitKey(0) - if key == ord('n'): - change_image(counte) - elif key == ord('q'): - cv2.destroyAllWindows() - - elif key == ord("c"): - img_name = "../saved/opencv_frame.png" - cv2.imwrite(img_name, image) - print("{} written!".format(img_name)) - - image = cv2.imread(img_name) - cv2.imshow("Frame", image) - key = cv2.waitKey(0) - os.remove("../saved/opencv_frame.png") - if key == ord("s"): - user_name = input("enter name") - imgdir = f"../saved/{user_name}.png" - cv2.imwrite(imgdir, image) - image_filtering_face(["../assest/tongue.png"],"../assest/face.jpg",[57],[0.6],[1.2],[-25],[0]) - if key == ord("e"): - image_filtering_face(["../assest/tongue.png"],"../assest/face.jpg",[57],[0.6],[1.2],[-25],[0]) + image = cv2.imread(path_img) + print("except") + return image + + # key = cv2.waitKey(0) + # if key == ord('n'): + # change_image(counte) + # elif key == ord('q'): + # cv2.destroyAllWindows() + # + # elif key == ord("c"): + # img_name = "../saved/opencv_frame.png" + # cv2.imwrite(img_name, image) + # print("{} written!".format(img_name)) + + # image = cv2.imread(img_name) + # cv2.imshow("Frame", image) + # key = cv2.waitKey(0) + # os.remove("../saved/opencv_frame.png") + # if key == ord("s"): + # user_name = input("enter name") + # imgdir = f"../saved/{user_name}.png" + # cv2.imwrite(imgdir, image) + # image_filtering_face(["../assest/tongue.png"],"../assest/face.jpg",[57],[0.6],[1.2],[-25],[0]) + # if key == ord("e"): + # image_filtering_face(["../assest/tongue.png"],"../assest/face.jpg",[57],[0.6],[1.2],[-25],[0]) def filter(image,gray_frame,faces,filter_image1,center,width,height,up=0,left=0): - predictor_path = r"../assest/shape_predictor_68_face_landmarks.dat" + predictor_path = "../assest/shape_predictor_68_face_landmarks.dat" predictor = dlib.shape_predictor(predictor_path) - for face in faces: landmarks = predictor(gray_frame, face) @@ -88,8 +86,10 @@ def filter(image,gray_frame,faces,filter_image1,center,width,height,up=0,left=0) image[top_left[1]: top_left[1] + filter_height, top_left[0]: top_left[0] + filter_width,:] = final_filter - - cv2.imshow("Frame", image) + print("filter1") + return image + # cv2.imshow("Frame", image) + # key = cv2.waitKey(0) def change_image(i): image_filtering_face( @@ -102,4 +102,4 @@ def change_image(i): change_filter[i]['counte'] ) if __name__ == "__main__": - image_filtering_face(["../assest/tongue.png"],[57],[0.6],[1.2],[-25],[0]) \ No newline at end of file + image_filtering_face(["../assest/tongue.png"],"../assest/Face2.jpg",[57],[0.6],[1.2],[-25],[0]) \ No newline at end of file diff --git a/filters_live/__pycache__/change_filter.cpython-39.pyc b/filters_live/__pycache__/change_filter.cpython-39.pyc deleted file mode 100644 index 984ad34..0000000 Binary files a/filters_live/__pycache__/change_filter.cpython-39.pyc and /dev/null differ diff --git a/filters_live/video_filtering_face.py b/filters_live/video_filtering_face.py index edb3e73..f35fff4 100644 --- a/filters_live/video_filtering_face.py +++ b/filters_live/video_filtering_face.py @@ -2,74 +2,49 @@ import numpy as np import dlib from math import hypot -from change_filter import change_filter +from filters_live.change_filter import change_filter +import mediapipe as mp + import os -# change_filter =[{'filter':['../assest/hair9.png','../assest/ghoul2.png'],'center':[27,66],'width':[1.5,1],'height':[1,1],'up':[100,20],'left':[0,0]}] cap = cv2.VideoCapture(0) _, frame = cap.read() rows, cols, _ = frame.shape filter1 = np.zeros((rows, cols), np.uint8) detector = dlib.get_frontal_face_detector() -predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") +predictor = dlib.shape_predictor("../filters_live/shape_predictor_68_face_landmarks.dat") counte=0 -def video_filtering_face(path,center,width,height,up,left,counte=0): +def video_filtering_face(path,center,width,height,up,left,path_back,blur,filter_face,background_face): filter_image = [] for i in path: filter_image.append(cv2.imread(i)) - - while cap.isOpened(): - filter1 = np.zeros((rows, cols), np.uint8) - - _, frame = cap.read() - filter1.fill(0) - gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) - faces = detector(frame) - if faces: - try: + # + filter1 = np.zeros((rows, cols), np.uint8) + _, frame = cap.read() + filter1.fill(0) + gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) + faces = detector(frame) + if faces: + try: + if filter_face: for i in range(len(path)): filter(frame,gray_frame,faces,filter_image[i],center[i],width[i],height[i],up[i],left[i]) - except: - _, frame = cap.read() - cv2.imshow("Frame", frame) - else: + if background_face: + frame =background(frame,path_back,blur ) + + except: _, frame = cap.read() - cv2.imshow("Frame", frame) - key = cv2.waitKey(1) - if key == ord('n'): - # print('first : ' ,i) - change_image(counte) - # print('second : ', i) - elif key == ord('q'): - cap.release() - cv2.destroyAllWindows() - elif key ==ord("c"): - img_name = "../saved/opencv_frame.png" - cv2.imwrite(img_name, frame) - # print("{} written!".format(img_name)) - break - frames = cv2.imread(img_name) - cv2.imshow("Frame", frames) - key = cv2.waitKey(0) - os.remove("../saved/opencv_frame.png") - if key ==ord("s"): - user_name = input("enter name") - imgdir = f"../saved/{user_name}.png" - cv2.imwrite(imgdir, frames) - video_filtering_face(["../assest/tongue.png"], [57], [0.6], [1.2], [-25], [0]) - if key == ord("e"): - video_filtering_face(["../assest/tongue.png"], [57], [0.6], [1.2], [-25], [0]) + else: + _, frame = cap.read() + return frame def filter(frame,gray_frame,faces,filter_image1,center,width,height,up=0,left=0): for face in faces: landmarks = predictor(gray_frame, face) - # filter coordinates - # top_filter = (landmarks.part(27).x+10, landmarks.part(24).y+10) - # if yahia: - # up = (landmarks.part(8).y - landmarks.part(62).y) * 3 + center_filter = (landmarks.part(center).x-left, landmarks.part(center).y-up) left_filter = (landmarks.part(4).x, landmarks.part(4).y) right_filter = (landmarks.part(14).x, landmarks.part(14).y) @@ -78,27 +53,26 @@ def filter(frame,gray_frame,faces,filter_image1,center,width,height,up=0,left=0) left_filter[1] - right_filter[1]) * width) filter_height = int(filter_width * height) - # New filter position top_left = (int(center_filter[0] - filter_width / 2), int(center_filter[1] - filter_height / 2)) bottom_right = (int(center_filter[0] + filter_width / 2), int(center_filter[1] + filter_height / 2)) - # Adding the new filter - # coloring + filtery = cv2.resize(filter_image1, (filter_width, filter_height)) filtery_gray = cv2.cvtColor(filtery, cv2.COLOR_BGR2GRAY) _, filter1 = cv2.threshold(filtery_gray, 25, 255, cv2.THRESH_BINARY_INV) filter_area = frame[top_left[1]: top_left[1] + filter_height, top_left[0]: top_left[0] + filter_width] + filter_area_no_filter = cv2.bitwise_and(filter_area, filter_area, mask=filter1) final_filter = cv2.add(filter_area_no_filter, filtery) frame[top_left[1]: top_left[1] + filter_height, top_left[0]: top_left[0] + filter_width,:] = final_filter - cv2.imshow("Frame", frame) + # cv2.imshow("Frame", frame) def change_image(i): video_filtering_face( @@ -110,5 +84,41 @@ def change_image(i): change_filter[i]['left'], change_filter[i]['counte'] ) + +mp_selfie_segmentation = mp.solutions.selfie_segmentation +fsize = (520, 720) +def background(frame,path,blur =1): + + + scene = cv2.imread(path) # read the scene image + scene = cv2.blur(scene, (blur, blur)) + scene = cv2.resize(scene, (fsize[1], fsize[0])) # resize scene to the size of frame + with mp_selfie_segmentation.SelfieSegmentation(model_selection=1) as selfie_seg: + bg_image = scene + # ret, frame = cap.read() + # if not ret: + # print("Error reading frame...") + + frame = cv2.resize(frame, (fsize[1], fsize[0])) + + # flip it to look like selfie camera + frame = cv2.flip(frame, 1) + + # get rgb image to pass that on selfie segmentation + rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) + + # process it! + results = selfie_seg.process(rgb) + + # get the condition from result's segmentation mask + condition = np.stack((results.segmentation_mask,) * 3, axis=-1) > 0.1 + + # apply background change if condition matches + output_image = np.where(condition, frame, bg_image) + return output_image + + cam.release() + cv2.destroyAllWindows() + if __name__ == "__main__": video_filtering_face(["../assest/tongue.png"],[57],[0.6],[1.2],[-25],[0]) \ No newline at end of file diff --git a/main.py b/main.py index 8fa7f38..e69de29 100644 --- a/main.py +++ b/main.py @@ -1,17 +0,0 @@ -import cv2 -import numpy as np - -import dlib -from filters.filter1 import filtering -from filters.dogfilter import filteringdog -# Loading Camera and Nose image and Creating mask -cap = cv2.VideoCapture(0) -_, frame = cap.read() -# print(frame) -rows, cols, _ = frame.shape -filter1 = np.zeros((rows, cols), np.uint8) -while True: - filteringdog(cap,rows, cols) - key = cv2.waitKey(1) - if key == 27: - break diff --git a/saved/b.png b/saved/b.png deleted file mode 100644 index 23201b2..0000000 Binary files a/saved/b.png and /dev/null differ diff --git a/saved/hi.png b/saved/hi.png deleted file mode 100644 index ff6562f..0000000 Binary files a/saved/hi.png and /dev/null differ