From dc49ba89c2a3fd4fabfc8c16a70f02bb37925302 Mon Sep 17 00:00:00 2001 From: gnany07 Date: Mon, 28 Oct 2019 00:09:34 +0530 Subject: [PATCH 1/3] Update PyFry.py Fixed --- PyFry.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/PyFry.py b/PyFry.py index 1bc7fec..6c3ec2a 100644 --- a/PyFry.py +++ b/PyFry.py @@ -47,20 +47,22 @@ def crushAndBack(img): img = img.resize((int(w ** .90), int(h ** .90)), resample = Image.BICUBIC) img = img.resize((w,h), resample = Image.BICUBIC) return img + def addFlare(img): ''' Initialising dlib for frontal facial features ''' flare = Image.open('flare.png') detect = dlib.get_frontal_face_detector() - predict = dlib.shape_predictor("assets\shape_predictor_68_face_landmarks.dat") + face_landmarks_file = "assets/shape_predictor_68_face_landmarks.dat" + face_landmarks_file = os.path.abspath(face_landmarks_file) + predict = dlib.shape_predictor(face_landmarks_file) (lS, lE) = face_utils.FACIAL_LANDMARKS_68_IDXS["left_eye"] (rS, rE) = face_utils.FACIAL_LANDMARKS_68_IDXS["right_eye"] - imgCV = cv2.imread('test.jpg') + imgCV = cv2.imread('temp.jpg') #imgCV = cv2.imread('test2.jpg') - gray = cv2.cvtColor(imgCV, cv2.COLOR_BGR2GRAY) subjects = detect(gray, 0) @@ -111,6 +113,7 @@ def main(): img = img.convert('RGB') img = crushAndBack(img) img = generateHue(img) + img.save('temp.jpg') img = addFlare(img) img.show() From 759fd1c452702c0f32d2c17093c414df8bd0fe18 Mon Sep 17 00:00:00 2001 From: gnany07 Date: Wed, 30 Oct 2019 00:57:59 +0530 Subject: [PATCH 2/3] Update PyFry.py Added a basic gui using tkinter in python,made relevant changes to hit proper functions --- PyFry.py | 164 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 113 insertions(+), 51 deletions(-) diff --git a/PyFry.py b/PyFry.py index 6c3ec2a..468bbae 100644 --- a/PyFry.py +++ b/PyFry.py @@ -1,5 +1,6 @@ import cv2 from PIL import Image, ImageOps, ImageEnhance +from PIL import ImageTk import os from utils.utils import Colors from imutils import face_utils @@ -12,56 +13,58 @@ -> Detecting eye coordinates and applying the deepfry eye flare in the center::DONE ''' -def userInput(): - #Allowing user to choose the image that has to be deepfried - root = tk.Tk() - root.withdraw() - global filepath - filepath = list(root.tk.splitlist(filedialog.askopenfilenames(title="PyFry - Choose Image"))) - print("picture location = ",filepath) - - +# def userInput(): +# #Allowing user to choose the image that has to be deepfried +# root = tk.Tk() +# root.withdraw() +# global filepath +# filepath = list(root.tk.splitlist(filedialog.askopenfilenames(title="PyFry - Choose Image"))) +# print("picture location = ",filepath) + + def irisCoords(eye): - #Finding the center point of the eye using the average outer extremes average of the eyes - mid = (eye[0] +eye[3])/2 + # Finding the center point of the eye using the average outer extremes average of the eyes + mid = (eye[0] + eye[3])/2 mid = (int(mid[0]), int(mid[1])) return mid + def generateHue(img): - #Generating and increasing prominency of red band of the image + # Generating and increasing prominency of red band of the image img = img.convert('RGB') - red = img.split()[0] #(R,G,B) + red = img.split()[0] # (R,G,B) red = ImageEnhance.Contrast(red).enhance(2.0) red = ImageEnhance.Brightness(red).enhance(1.5) red = ImageOps.colorize(red, Colors.RED, Colors.YELLOW) img = Image.blend(img, red, 0.77) - #Keeping a 100% sharpness value for now, But would probably be better with a higher sharpness value + # Keeping a 100% sharpness value for now, But would probably be better with a higher sharpness value img = ImageEnhance.Sharpness(img).enhance(150) return img + def crushAndBack(img): img = img.convert('RGB') - w,h = img.width, img.height + w, h = img.width, img.height img = img.resize((int(w ** .95), int(h ** .95)), resample=Image.LANCZOS) - img = img.resize((int(w ** .90), int(h ** .90)), resample = Image.BILINEAR) - img = img.resize((int(w ** .90), int(h ** .90)), resample = Image.BICUBIC) - img = img.resize((w,h), resample = Image.BICUBIC) + img = img.resize((int(w ** .90), int(h ** .90)), resample=Image.BILINEAR) + img = img.resize((int(w ** .90), int(h ** .90)), resample=Image.BICUBIC) + img = img.resize((w, h), resample=Image.BICUBIC) return img + def addFlare(img): ''' Initialising dlib for frontal facial features ''' flare = Image.open('flare.png') detect = dlib.get_frontal_face_detector() - face_landmarks_file = "assets/shape_predictor_68_face_landmarks.dat" + face_landmarks_file = "assets/shape_predictor_68_face_landmarks.dat" face_landmarks_file = os.path.abspath(face_landmarks_file) predict = dlib.shape_predictor(face_landmarks_file) (lS, lE) = face_utils.FACIAL_LANDMARKS_68_IDXS["left_eye"] (rS, rE) = face_utils.FACIAL_LANDMARKS_68_IDXS["right_eye"] - imgCV = cv2.imread('temp.jpg') - #imgCV = cv2.imread('test2.jpg') + # imgCV = cv2.imread('test2.jpg') gray = cv2.cvtColor(imgCV, cv2.COLOR_BGR2GRAY) subjects = detect(gray, 0) @@ -77,51 +80,110 @@ def addFlare(img): This is used to find the basic coordinates of the area in which the flare image will be pasted ''' - rn=(rightEye[4][0]-rightEye[0][0])*3 - ln=(leftEye[4][0]-leftEye[0][0])*3 + rn = (rightEye[4][0]-rightEye[0][0])*3 + ln = (leftEye[4][0]-leftEye[0][0])*3 + + rec0 = (leftEye[1][0]-ln, leftEye[1][1]-ln) + rec1 = (leftEye[4][0]+ln, leftEye[4][1]+ln) - rec0=(leftEye[1][0]-ln,leftEye[1][1]-ln) - rec1=(leftEye[4][0]+ln,leftEye[4][1]+ln) - - rec2=(rightEye[1][0]-rn,rightEye[1][1]-rn) - rec3=(rightEye[4][0]+rn,rightEye[4][1]+rn) - - print("Area for left eye",rec0,rec1) - print("Area for right eye",rec2,rec3) + rec2 = (rightEye[1][0]-rn, rightEye[1][1]-rn) + rec3 = (rightEye[4][0]+rn, rightEye[4][1]+rn) + + print("Area for left eye", rec0, rec1) + print("Area for right eye", rec2, rec3) """ Area Assignment for left eye and right eye""" - areaLeft=(rec0[0],rec0[1],rec1[0],rec1[1]) - areaRight=(rec2[0],rec2[1],rec3[0],rec3[1]) - + areaLeft = (rec0[0], rec0[1], rec1[0], rec1[1]) + areaRight = (rec2[0], rec2[1], rec3[0], rec3[1]) + """ Resizing the flare image to fit the area""" - flareLeft=flare.resize((rec1[0]-rec0[0],rec1[1]-rec0[1])) - flareRight=flare.resize((rec3[0]-rec2[0],rec3[1]-rec2[1])) - + flareLeft = flare.resize((rec1[0]-rec0[0], rec1[1]-rec0[1])) + flareRight = flare.resize((rec3[0]-rec2[0], rec3[1]-rec2[1])) + """Pasting the flare image on the area. Third parameter is an alpha channel that provides transparency for the png""" - img.paste(flareLeft,areaLeft,flareLeft) - img.paste(flareRight,areaRight,flareRight) + img.paste(flareLeft, areaLeft, flareLeft) + img.paste(flareRight, areaRight, flareRight) return img -def main(): - userInput() - - for img_path in filepath: +def pyfry(): + # grab a reference to the image panels + global panelA, panelB + print("panelA set to global variable") + # open a file chooser dialog and allow the user to select an input + # image + img_path = filedialog.askopenfilename() + print(img_path) + + # ensure a file path was selected + if len(img_path) > 0: img = Image.open(img_path) - #img = Image.opne('test2.jpg') - img = img.convert('RGB') img = crushAndBack(img) img = generateHue(img) img.save('temp.jpg') img = addFlare(img) - - img.show() - #img.save('output2.jpg') + + # img.show() + # img.save('output2.jpg') filename = os.path.splitext(os.path.basename(img_path))[0] img.save('%s_output.jpg' % filename) print("output saved as %s_output.jpg" % filename) - -if __name__ == '__main__': - main() + original_image = cv2.imread(img_path) + deep_fried_img_path = '%s_output.jpg' % filename + deep_fried_img = cv2.imread(deep_fried_img_path) + # OpenCV represents images in BGR order; however PIL represents + # images in RGB order, so we need to swap the channels + original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB) + deep_fried_img = cv2.cvtColor(deep_fried_img, cv2.COLOR_BGR2RGB) + + # convert the images to PIL format... + original_image = Image.fromarray(original_image) + deep_fried_img = Image.fromarray(deep_fried_img) + + # ...and then to ImageTk format + original_image = ImageTk.PhotoImage(original_image) + deep_fried_img = ImageTk.PhotoImage(deep_fried_img) + + # if the panels are None, initialize them + if panelA is None or panelB is None: + # the first panel will store our original image + panelA = tk.Label(image=original_image) + panelA.image = original_image + panelA.pack(side="left", padx=10, pady=10) + + # while the second panel will store the edge map + panelB = tk.Label(image=deep_fried_img) + panelB.image = deep_fried_img + panelB.pack(side="right", padx=10, pady=10) + + # otherwise, update the image panels + else: + # update the pannels + panelA.configure(image=original_image) + panelB.configure(image=deep_fried_img) + panelA.image = original_image + panelB.image = deep_fried_img + + +# userInput() +# initialize the window toolkit along with the two image panels +root = tk.Tk() +root.title("Welcome to PyFry") + +# root.geometry("600x100") +print("panelA defined as null") +panelA = None +panelB = None + +# create a button, then when pressed, will trigger a file chooser +# dialog and allow the user to select an input image; then add the +# button the GUI +btn = tk.Button(root, text="click here to select new image to deep fry!!!", command=pyfry) +btn.pack(side="bottom", fill="both", expand="yes", padx="10", pady="10") + +# kick off the GUI +root.mainloop() + + \ No newline at end of file From b9bad5263bdf47dbe3d39b106f1a729317894cb0 Mon Sep 17 00:00:00 2001 From: gnany07 Date: Wed, 30 Oct 2019 01:00:43 +0530 Subject: [PATCH 3/3] Update PyFry.py removed debug print statements --- PyFry.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/PyFry.py b/PyFry.py index 468bbae..eb2378d 100644 --- a/PyFry.py +++ b/PyFry.py @@ -110,7 +110,6 @@ def addFlare(img): def pyfry(): # grab a reference to the image panels global panelA, panelB - print("panelA set to global variable") # open a file chooser dialog and allow the user to select an input # image img_path = filedialog.askopenfilename() @@ -173,7 +172,6 @@ def pyfry(): root.title("Welcome to PyFry") # root.geometry("600x100") -print("panelA defined as null") panelA = None panelB = None