Spaces:
Running
Running
Alican Akca
commited on
Commit
·
914a2f2
1
Parent(s):
bd72a39
GIF and Video Processing
Browse files- app.py +2 -1
- methods/img2pixl.py +63 -53
app.py
CHANGED
|
@@ -57,4 +57,5 @@ gr.Interface(fn = initilize,
|
|
| 57 |
inputs = inputs,
|
| 58 |
outputs = outputs,
|
| 59 |
title=title,
|
| 60 |
-
description=description).launch()
|
|
|
|
|
|
| 57 |
inputs = inputs,
|
| 58 |
outputs = outputs,
|
| 59 |
title=title,
|
| 60 |
+
description=description).launch()
|
| 61 |
+
|
methods/img2pixl.py
CHANGED
|
@@ -1,61 +1,71 @@
|
|
| 1 |
-
import os
|
| 2 |
import cv2
|
| 3 |
-
import torch
|
| 4 |
-
import warnings
|
| 5 |
import numpy as np
|
| 6 |
-
import gradio as gr
|
| 7 |
-
import paddlehub as hub
|
| 8 |
from PIL import Image
|
| 9 |
-
from methods.img2pixl import pixL
|
| 10 |
-
from examples.pixelArt.combine import combine
|
| 11 |
-
from methods.media import Media
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
face2paint = torch.hub.load("bryandlee/animegan2-pytorch:main", "face2paint", device=device, size=512)
|
| 18 |
-
model = torch.hub.load("bryandlee/animegan2-pytorch", "generator", device=device).eval()
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
return Media().split(media.name,pixel_size)
|
| 27 |
else:
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
|
|
|
|
|
|
|
|
| 1 |
import cv2
|
|
|
|
|
|
|
| 2 |
import numpy as np
|
|
|
|
|
|
|
| 3 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
class pixL:
|
| 6 |
+
#Author: Alican Akca
|
| 7 |
+
def __init__(self,numOfSquaresW = None, numOfSquaresH= None, size = [False, (512,512)],square = 6,ImgH = None,ImgW = None,images = [],background_image = None):
|
| 8 |
+
self.images = images
|
| 9 |
+
self.size = size
|
| 10 |
+
self.ImgH = ImgH
|
| 11 |
+
self.ImgW = ImgW
|
| 12 |
+
self.square = square
|
| 13 |
+
self.numOfSquaresW = numOfSquaresW
|
| 14 |
+
self.numOfSquaresH = numOfSquaresH
|
| 15 |
|
| 16 |
+
def preprocess(self):
|
| 17 |
+
for image in self.images:
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
size = (image.shape[0] - (image.shape[0] % 4), image.shape[1] - (image.shape[1] % 4))
|
| 20 |
+
image = cv2.resize(image, size)
|
| 21 |
+
image = cv2.cvtColor(image.astype(np.uint8), cv2.COLOR_BGR2RGB)
|
| 22 |
+
|
| 23 |
+
if len(self.images) == 1:
|
| 24 |
+
return self.images[0]
|
|
|
|
| 25 |
else:
|
| 26 |
+
return self.images
|
| 27 |
+
|
| 28 |
+
def toThePixL(self,images, pixel_size):
|
| 29 |
+
self.images = []
|
| 30 |
+
self.square = pixel_size
|
| 31 |
+
for image in images:
|
| 32 |
+
image = Image.fromarray(image)
|
| 33 |
+
image = image.convert("RGB")
|
| 34 |
+
self.ImgW, self.ImgH = image.size
|
| 35 |
+
self.images.append(pixL.epicAlgorithm(self, image))
|
| 36 |
+
|
| 37 |
+
return pixL.preprocess(self)
|
| 38 |
+
|
| 39 |
+
def numOfSquaresFunc(self):
|
| 40 |
+
self.numOfSquaresW = round((self.ImgW / self.square) + 1)
|
| 41 |
+
self.numOfSquaresH = round((self.ImgH / self.square) + 1)
|
| 42 |
+
|
| 43 |
+
def epicAlgorithm(self, image):
|
| 44 |
+
pixValues = []
|
| 45 |
+
pixL.numOfSquaresFunc(self)
|
| 46 |
+
|
| 47 |
+
for j in range(1,self.numOfSquaresH):
|
| 48 |
+
|
| 49 |
+
for i in range(1,self.numOfSquaresW):
|
| 50 |
+
|
| 51 |
+
pixValues.append((image.getpixel((
|
| 52 |
+
i * self.square - self.square//2,
|
| 53 |
+
j * self.square - self.square//2)),
|
| 54 |
+
(i * self.square - self.square//2,
|
| 55 |
+
j * self.square - self.square//2)))
|
| 56 |
+
|
| 57 |
+
background = 255 * np.ones(shape=[self.ImgH - self.square,
|
| 58 |
+
self.ImgW - self.square*2, 3],
|
| 59 |
+
dtype=np.uint8)
|
| 60 |
+
|
| 61 |
+
for pen in range(len(pixValues)):
|
| 62 |
+
|
| 63 |
+
cv2.rectangle(background,
|
| 64 |
+
pt1=(pixValues[pen][1][0] - self.square,pixValues[pen][1][1] - self.square),
|
| 65 |
+
pt2=(pixValues[pen][1][0] + self.square,pixValues[pen][1][1] + self.square),
|
| 66 |
+
color=(pixValues[pen][0][2],pixValues[pen][0][1],pixValues[pen][0][0]),
|
| 67 |
+
thickness=-1)
|
| 68 |
+
background = np.array(background).astype(np.uint8)
|
| 69 |
+
background = cv2.resize(background, (self.ImgW,self.ImgH), interpolation = cv2.INTER_AREA)
|
| 70 |
|
| 71 |
+
return background
|