Catmeow commited on
Commit
f55f792
β€’
1 Parent(s): 851fb3f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -111
app.py CHANGED
@@ -1,117 +1,8 @@
1
- import os
2
- os.system("pip install dlib")
3
- import sys
4
- import face_detection
5
- from PIL import Image, ImageOps, ImageFile
6
- import numpy as np
7
- import cv2 as cv
8
- import torch
9
  import gradio as gr
 
10
 
11
- torch.set_grad_enabled(False)
12
-
13
- device = "cuda" if torch.cuda.is_available() else "cpu"
14
- model = torch.hub.load("bryandlee/animegan2-pytorch:main", "generator", device=device).eval()
15
- model2 = torch.hub.load("AK391/animegan2-pytorch:main", "generator", pretrained="face_paint_512_v1", device=device).eval()
16
- face2paint = torch.hub.load("bryandlee/animegan2-pytorch:main", "face2paint", device=device)
17
- image_format = "png" #@param ["jpeg", "png"]
18
-
19
- def unsharp_mask(image, kernel_size=(5, 5), sigma=1.0, amount=2.0, threshold=0):
20
- """Return a sharpened version of the image, using an unsharp mask."""
21
- blurred = cv.GaussianBlur(image, kernel_size, sigma)
22
- sharpened = float(amount + 1) * image - float(amount) * blurred
23
- sharpened = np.maximum(sharpened, np.zeros(sharpened.shape))
24
- sharpened = np.minimum(sharpened, 255 * np.ones(sharpened.shape))
25
- sharpened = sharpened.round().astype(np.uint8)
26
- if threshold > 0:
27
- low_contrast_mask = np.absolute(image - blurred) < threshold
28
- np.copyto(sharpened, image, where=low_contrast_mask)
29
- return sharpened
30
-
31
- def normPRED(d):
32
- ma = np.max(d)
33
- mi = np.min(d)
34
-
35
- dn = (d-mi)/(ma-mi)
36
-
37
- return dn
38
-
39
- def array_to_np(array_in):
40
- array_in = normPRED(array_in)
41
- array_in = np.squeeze(255.0*(array_in))
42
- array_in = np.transpose(array_in, (1, 2, 0))
43
- return array_in
44
-
45
- def array_to_image(array_in):
46
- array_in = normPRED(array_in)
47
- array_in = np.squeeze(255.0*(array_in))
48
- array_in = np.transpose(array_in, (1, 2, 0))
49
- im = Image.fromarray(array_in.astype(np.uint8))
50
- return im
51
-
52
-
53
- def image_as_array(image_in):
54
- image_in = np.array(image_in, np.float32)
55
- tmpImg = np.zeros((image_in.shape[0],image_in.shape[1],3))
56
- image_in = image_in/np.max(image_in)
57
- if image_in.shape[2]==1:
58
- tmpImg[:,:,0] = (image_in[:,:,0]-0.485)/0.229
59
- tmpImg[:,:,1] = (image_in[:,:,0]-0.485)/0.229
60
- tmpImg[:,:,2] = (image_in[:,:,0]-0.485)/0.229
61
- else:
62
- tmpImg[:,:,0] = (image_in[:,:,0]-0.485)/0.229
63
- tmpImg[:,:,1] = (image_in[:,:,1]-0.456)/0.224
64
- tmpImg[:,:,2] = (image_in[:,:,2]-0.406)/0.225
65
-
66
- tmpImg = tmpImg.transpose((2, 0, 1))
67
- image_out = np.expand_dims(tmpImg, 0)
68
- return image_out
69
-
70
- # detect a face
71
- def find_aligned_face(image_in, size=400):
72
- aligned_image, n_faces, quad = face_detection.align(image_in, face_index=0, output_size=size)
73
- return aligned_image, n_faces, quad
74
-
75
- # clip the face, return array
76
- def align_first_face(image_in, size=400):
77
- aligned_image, n_faces, quad = find_aligned_face(image_in,size=size)
78
- if n_faces == 0:
79
- try:
80
- image_in = ImageOps.exif_transpose(image_in)
81
- except:
82
- print("exif problem, not rotating")
83
- image_in = image_in.resize((size, size))
84
- im_array = image_as_array(image_in)
85
- else:
86
- im_array = image_as_array(aligned_image)
87
-
88
- return im_array
89
-
90
- def img_concat_h(im1, im2):
91
- dst = Image.new('RGB', (im1.width + im2.width, im1.height))
92
- dst.paste(im1, (0, 0))
93
- dst.paste(im2, (im1.width, 0))
94
- return dst
95
-
96
- def paintface(img: Image.Image,size: int) -> Image.Image:
97
- aligned_img = align_first_face(img,size)
98
- if aligned_img is None:
99
- output=None
100
- else:
101
- im_in = array_to_image(aligned_img).convert("RGB")
102
- im_out1 = face2paint(model, im_in, side_by_side=False)
103
- im_out2 = face2paint(model2, im_in, side_by_side=False)
104
-
105
- output = img_concat_h(im_out1, im_out2)
106
- return output
107
-
108
- def generate(img):
109
- out = paintface(img, 400)
110
- return out
111
-
112
-
113
  title = "Face from Photo into Handed-paint"
114
- description = "Upload a photo, this Ai will detect and transfer only the main face into cartoon/anime handed painting style. (If cannot detect face, try the edit button to crop the photo manually on the right corner of the picture.)"
115
  article = "Examples are from Internet"
116
 
117
  Example=[['Example01.jpg'],['Example02.jpg'],['Example03.jpg']]
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from paintingface import generate
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  title = "Face from Photo into Handed-paint"
5
+ description = "Upload a photo, this Ai will detect and transfer only the main face into cartoon/anime handed-painting style. (If cannot detect a face, try the edit button on the right corner of the picture to crop the photo manually.)"
6
  article = "Examples are from Internet"
7
 
8
  Example=[['Example01.jpg'],['Example02.jpg'],['Example03.jpg']]