Spaces:
Runtime error
Runtime error
init
Browse files- app.py +1 -4
- p2c/test.py +10 -19
app.py
CHANGED
|
@@ -19,7 +19,7 @@ import cv2
|
|
| 19 |
from io import BytesIO
|
| 20 |
sys.path.insert(0, 'p2c')
|
| 21 |
|
| 22 |
-
from
|
| 23 |
|
| 24 |
|
| 25 |
ORIGINAL_REPO_URL = 'https://github.com/minivision-ai/photo2cartoon'
|
|
@@ -47,9 +47,6 @@ def parse_args() -> argparse.Namespace:
|
|
| 47 |
parser.add_argument('--allow-screenshot', action='store_true')
|
| 48 |
return parser.parse_args()
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
def run(
|
| 54 |
image,
|
| 55 |
p2c : Photo2Cartoon,
|
|
|
|
| 19 |
from io import BytesIO
|
| 20 |
sys.path.insert(0, 'p2c')
|
| 21 |
|
| 22 |
+
from test import Photo2Cartoon
|
| 23 |
|
| 24 |
|
| 25 |
ORIGINAL_REPO_URL = 'https://github.com/minivision-ai/photo2cartoon'
|
|
|
|
| 47 |
parser.add_argument('--allow-screenshot', action='store_true')
|
| 48 |
return parser.parse_args()
|
| 49 |
|
|
|
|
|
|
|
|
|
|
| 50 |
def run(
|
| 51 |
image,
|
| 52 |
p2c : Photo2Cartoon,
|
p2c/test.py
CHANGED
|
@@ -7,25 +7,22 @@ import argparse
|
|
| 7 |
from utils import Preprocess
|
| 8 |
|
| 9 |
|
| 10 |
-
parser = argparse.ArgumentParser()
|
| 11 |
-
parser.add_argument('--photo_path', type=str, help='input photo path')
|
| 12 |
-
parser.add_argument('--save_path', type=str, help='cartoon save path')
|
| 13 |
-
args = parser.parse_args()
|
| 14 |
-
|
| 15 |
-
os.makedirs(os.path.dirname(args.save_path), exist_ok=True)
|
| 16 |
-
|
| 17 |
class Photo2Cartoon:
|
| 18 |
def __init__(self):
|
| 19 |
self.pre = Preprocess()
|
| 20 |
self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 21 |
self.net = ResnetGenerator(ngf=32, img_size=256, light=True).to(self.device)
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
self.net.load_state_dict(params['genA2B'])
|
| 26 |
print('[Step1: load weights] success!')
|
| 27 |
|
| 28 |
-
def inference(self,
|
|
|
|
|
|
|
| 29 |
# face alignment and segmentation
|
| 30 |
face_rgba = self.pre.process(img)
|
| 31 |
if face_rgba is None:
|
|
@@ -49,15 +46,9 @@ class Photo2Cartoon:
|
|
| 49 |
cartoon = np.transpose(cartoon.cpu().numpy(), (1, 2, 0))
|
| 50 |
cartoon = (cartoon + 1) * 127.5
|
| 51 |
cartoon = (cartoon * mask + 255 * (1 - mask)).astype(np.uint8)
|
| 52 |
-
cartoon = cv2.cvtColor(cartoon, cv2.COLOR_RGB2BGR)
|
| 53 |
print('[Step3: photo to cartoon] success!')
|
| 54 |
return cartoon
|
| 55 |
|
| 56 |
|
| 57 |
-
|
| 58 |
-
img = cv2.cvtColor(cv2.imread(args.photo_path), cv2.COLOR_BGR2RGB)
|
| 59 |
-
c2p = Photo2Cartoon()
|
| 60 |
-
cartoon = c2p.inference(img)
|
| 61 |
-
if cartoon is not None:
|
| 62 |
-
cv2.imwrite(args.save_path, cartoon)
|
| 63 |
-
print('Cartoon portrait has been saved successfully!')
|
|
|
|
| 7 |
from utils import Preprocess
|
| 8 |
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
class Photo2Cartoon:
|
| 11 |
def __init__(self):
|
| 12 |
self.pre = Preprocess()
|
| 13 |
self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 14 |
self.net = ResnetGenerator(ngf=32, img_size=256, light=True).to(self.device)
|
| 15 |
+
|
| 16 |
+
curPath = os.path.abspath(os.path.dirname(__file__))
|
| 17 |
+
|
| 18 |
+
#assert os.path.exists('./models/photo2cartoon_weights.pt'), "[Step1: load weights] Can not find 'photo2cartoon_weights.pt' in folder 'models!!!'"
|
| 19 |
+
params = torch.load(os.path.join(curPath, 'models/photo2cartoon_weights.pt'), map_location=self.device)
|
| 20 |
self.net.load_state_dict(params['genA2B'])
|
| 21 |
print('[Step1: load weights] success!')
|
| 22 |
|
| 23 |
+
def inference(self, in_path):
|
| 24 |
+
img = cv2.cvtColor(cv2.imread(in_path), cv2.COLOR_BGR2RGB)
|
| 25 |
+
|
| 26 |
# face alignment and segmentation
|
| 27 |
face_rgba = self.pre.process(img)
|
| 28 |
if face_rgba is None:
|
|
|
|
| 46 |
cartoon = np.transpose(cartoon.cpu().numpy(), (1, 2, 0))
|
| 47 |
cartoon = (cartoon + 1) * 127.5
|
| 48 |
cartoon = (cartoon * mask + 255 * (1 - mask)).astype(np.uint8)
|
| 49 |
+
#cartoon = cv2.cvtColor(cartoon, cv2.COLOR_RGB2BGR)
|
| 50 |
print('[Step3: photo to cartoon] success!')
|
| 51 |
return cartoon
|
| 52 |
|
| 53 |
|
| 54 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|