Toonify Scripts (#154)

* Toonify Script Added

* Update README.md

* Update toonify-API-2.py
This commit is contained in:
Chetan Pandey 2020-10-05 13:25:29 +05:30 committed by GitHub
parent 0a2ceccb4b
commit cef99f37d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 107 additions and 0 deletions

39
Toonify/README.md Normal file
View File

@ -0,0 +1,39 @@
## HOW TO USE
#### Using OpenCv ([link](./toonify-opencv.py))
- To just view the image
```python
python3 toonify-opencv.py -i img_path
```
- To view and download the image
```python
python3 toonify-opencv.py -i img_path -o download_path
```
#### Using Toonify-API(by DeepAI)
##### For local image ([link](./toonify-API-1.py))
```python
python3 toonify-API-1.py -i img_path -k api_key
```
##### For URLS ([link](./toonify-API-2.py))
```python
python3 toonify-API-2.py -i img_path -k api_key
```
> NOTE: The toonify image works well with .jpg format and might give some problem with other formats
For more details on toonify API:
[toonify API doc](https://deepai.org/machine-learning-model/toonify)

18
Toonify/toonify-API-1.py Normal file
View File

@ -0,0 +1,18 @@
import argparse
import requests
aq = argparse.ArgumentParser()
aq.add_argument('-i', '--input', required=True, help="input image path")
aq.add_argument('-k', '--apikey', required=True, help="api-key")
args = vars(aq.parse_args())
r = requests.post(
"https://api.deepai.org/api/toonify",
files={
'image': open(args['input'], 'rb'),
},
headers={'api-key': args['apikey']}
)
print(r.json()['output_url'])

18
Toonify/toonify-API-2.py Normal file
View File

@ -0,0 +1,18 @@
import argparse
import requests
aq = argparse.ArgumentParser()
aq.add_argument('-i', '--input', required=True, help="input image link")
aq.add_argument('-k', '--apikey', required=True, help="api-key")
args = vars(aq.parse_args())
r = requests.post(
"https://api.deepai.org/api/toonify",
files={
'image': args['input'],
},
headers={'api-key': args['apikey']}
)
print(r.json()['output_url'])

32
Toonify/toonify-opencv.py Normal file
View File

@ -0,0 +1,32 @@
# importing libraries
import cv2
import numpy as np
import argparse
aq = argparse.ArgumentParser()
aq.add_argument('-i', '--input', required=True, help="input image path")
aq.add_argument('-o', '--output', help="path where you want to download the image")
args = vars(aq.parse_args())
# reading image
img = cv2.imread(args['input'])
# Edges
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.medianBlur(gray, 5)
edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY, 9, 9)
# Cartoonization
color = cv2.bilateralFilter(img, 2, 250, 250)
cartoon = cv2.bitwise_or(color, color, mask=edges)
if(args['output']):
cv2.imwrite(args['output'], cartoon)
cv2.imshow("Cartoon", cartoon)
cv2.waitKey(0)
cv2.destroyAllWindows()