generated from kookmin-sw/cap-template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathimgcrawl2.py
66 lines (49 loc) · 1.7 KB
/
imgcrawl2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
62
63
from bs4 import BeautifulSoup
from urllib.request import urlopen
import urllib.request
from PIL import Image
from io import BytesIO
import numpy as np
import requests
import json
def crawl(url: str):
image_list = []
html = urlopen(url).read()
soup = BeautifulSoup(html, "html.parser")
only_text = soup.get_text()
images = soup.find_all('img')
for i, img in enumerate(images):
src = img.get('src')
#print(src)
if src == None:
continue
if not src.startswith('http'):
continue
if src.endswith('.svg') or '.gif' in src:
continue
result = requests.get(src)
img = Image.open(BytesIO(result.content))
img = img.convert("RGB")
img = img.resize((64,64))
data = np.asarray(img)
data = np.array(data)
data = data.astype("float") / 256
data = data.reshape(-1, 64, 64,3)
image_list.append(data)
return only_text, image_list
def get_mood_from_image(url: str):
_, image_list = crawl(url)
categories = ["anger","fear","joy","love","sadness","surprise"]
result_list = [0, 0, 0, 0, 0, 0]
if not image_list:
print("No image")
return
for img in image_list:
address = 'http://localhost:8501/v1/models/resnet152:predict'
data = json.dumps({'instances':img.tolist()})
result = requests.post(address, data=data)
predictions = json.loads(str(result.content, 'utf-8'))['predictions']
for prediction in predictions:
#print('New data category : ',categories[np.argmax(prediction)])
result_list[np.argmax(prediction)] += 1
print(categories[np.argmax(result_list)])