Skip to content

Commit 29b5e4a

Browse files
committed
last
1 parent ae33e66 commit 29b5e4a

File tree

7 files changed

+11239
-32
lines changed

7 files changed

+11239
-32
lines changed

Image_recognition/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ def train(**kwargs):
130130
if opt.load_model_path:
131131
# # 把所有的张量加载到CPU中
132132
# t.load(opt.load_model_path, map_location=lambda storage, loc: storage)
133+
# t.load(opt.load_model_path, map_location='cpu')
133134
# # 把所有的张量加载到GPU 1中
134135
# t.load(opt.load_model_path, map_location=lambda storage, loc: storage.cuda(1))
135136
# # 把张量从GPU 1 移动到 GPU 0

ai-tools/Sketch/sketh.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import numpy as np
2+
import argparse
3+
import cv2
4+
import imutils
5+
6+
import numpy as np
7+
import cv2
8+
9+
def order_points(pts):
10+
#四个点按照左上、右上、右下、左下
11+
12+
#pts 是四个点的列表
13+
rect = np.zeros((4,2),dtype="float32")
14+
15+
s = pts.sum(axis=1)
16+
rect[0] = pts[np.argmin(s)]
17+
rect[2] = pts[np.argmax(s)]
18+
19+
diff = np.diff(pts,axis = 1)
20+
rect[1] = pts[np.argmin(diff)]
21+
rect[3] = pts[np.argmax(diff)]
22+
23+
return rect
24+
25+
def four_point_transform(image, pts): #image 为需要透视变换的图像 pts为四个点
26+
27+
rect = order_points(pts) #四点排序
28+
(tl, tr, br, bl) = rect #方便计算
29+
30+
widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
31+
widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
32+
maxWidth = max(int(widthA), int(widthB))
33+
34+
heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
35+
heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
36+
maxHeight = max(int(heightA), int(heightB))
37+
38+
39+
dst = np.array([
40+
[0, 0],
41+
[maxWidth - 1, 0],
42+
[maxWidth - 1, maxHeight - 1],
43+
[0, maxHeight - 1]], dtype = "float32")
44+
45+
M = cv2.getPerspectiveTransform(rect, dst)
46+
warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))
47+
48+
return warped
49+
50+
# ap = argparse.ArgumentParser()
51+
# ap.add_argument('-i','--image',required=True,help="Path to image file")
52+
#
53+
# args = vars(ap.parse_args())
54+
55+
#flag: 获取了图像路径
56+
57+
image = cv2.imread('test.jpg')
58+
image = imutils.resize(image,height=500)
59+
60+
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) #灰度图像
61+
gray = cv2.GaussianBlur(gray,(5,5),0) #高斯模糊
62+
edged = cv2.Canny(gray,75,200) #边缘检测
63+
64+
65+
66+
# flag : Test1 = BLOCK
67+
print("STEP 1 Edge Detection")
68+
cv2.imshow("Edge",edged)
69+
cv2.waitKey(0)
70+
cv2.destroyAllWindows()

ai-tools/color_cloth/imagetone.py

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
import cv2
33
import math
44
import numpy as np
5-
import base64
65
import requests
7-
from io import BytesIO
6+
import time
87

98

109
def get_hue(a, b):
@@ -118,9 +117,9 @@ def rgb2hex(rgb):
118117
return color
119118

120119

121-
APP_ID = '15180965'
122-
API_KEY = 'Gjw3RzhDcMSS8RESUEiVNWkH'
123-
SECRET_KEY = 'pZVtDe5Z74cA2ropdsI3s3rGMrFmXH9N'
120+
APP_ID = '17887227'
121+
API_KEY = 'HXkluVYkuRL1PURlsa973vsl'
122+
SECRET_KEY = 'khKaBZEkN2EnDoeRkU18pGbBF71CH2Fd'
124123

125124

126125
class ImageColor:
@@ -130,19 +129,16 @@ def __init__(self, url=None, file_path=None):
130129
self.param = None
131130
self.url = url
132131
self.img = None
132+
self.response = None
133133

134134
# 读取图片
135135
def read_image(self):
136136
if self.file_path:
137137
self.img = cv2.imread(self.file_path)
138138
elif self.url:
139-
140-
cap = cv2.VideoCapture(self.url)
141-
ret, img = cap.read()
142-
if not ret:
143-
response = requests.get(self.url)
144-
image_numpy = np.asarray(bytearray(response.content), dtype="uint8")
145-
img = cv2.imdecode(image_numpy, cv2.IMREAD_COLOR)
139+
self.response = requests.get(self.url)
140+
image_numpy = np.asarray(bytearray(self.response.content), dtype="uint8")
141+
img = cv2.imdecode(image_numpy, cv2.IMREAD_COLOR)
146142
self.img = img
147143

148144
# 主体检测
@@ -156,22 +152,32 @@ def subject_detection(self):
156152
self.param = response['result']
157153
elif self.url:
158154
# 调用图像主体检测
159-
response = requests.get(self.url)
160155
options = dict()
161156
options["with_face"] = 0
162-
response = self.client.objectDetect(response.content, options)
163-
self.param = response['result']
157+
response = self.client.objectDetect(self.response.content, options)
158+
if 'result' in response:
159+
self.param = response['result']
164160

165161
# 主体参数裁剪图片
166162
def tailoring(self):
167-
initial_image = self.img[self.param['top']:self.param['top'] + self.param['height'],
168-
self.param['left']:self.param['left'] + self.param['width']]
163+
if self.param:
164+
self.img = self.img[self.param['top']:self.param['top'] + self.param['height'],
165+
self.param['left']:self.param['left'] + self.param['width']]
169166
# cv2.imshow('ai', initial_image) # 显示图片
170167
# cv2.waitKey(0)
171-
self.img = initial_image
172168

173169
# 颜色识别
174170
def color_recognition(self, expected_size=40, in_clusters=7, out_clusters=3):
171+
a = time.time()
172+
self.read_image()
173+
b = time.time()
174+
# print('读取图片',b-a)
175+
self.subject_detection()
176+
c = time.time()
177+
# print('主体检测', c - b)
178+
self.tailoring()
179+
d = time.time()
180+
# print('裁剪', d - c)
175181
height, width = self.img.shape[:2]
176182

177183
factor = math.sqrt(width * height / (expected_size * expected_size))
@@ -180,8 +186,6 @@ def color_recognition(self, expected_size=40, in_clusters=7, out_clusters=3):
180186
image = cv2.resize(self.img,
181187
(int(width / factor), int(height / factor)),
182188
interpolation=cv2.INTER_LINEAR)
183-
cv2.imshow('ai', image)
184-
cv2.waitKey(0)
185189
LAB_image = cv2.cvtColor(image, cv2.COLOR_BGR2Lab)
186190
frame_width = int(expected_size / 10 + 2)
187191
in_samples = []
@@ -311,13 +315,12 @@ def color_recognition(self, expected_size=40, in_clusters=7, out_clusters=3):
311315
color = ','.join('%s' % id for id in color)
312316
color_weight = round(proportions[i] / sum(proportions), 2)
313317
color_list.append((color_weight, color, rgb2hex(color)))
314-
# color_list = sorted(color_list, key=lambda d: d[0], reverse=True)
318+
color_list = sorted(color_list, key=lambda d: d[0], reverse=True)
319+
e = time.time()
320+
# print('识别',e-d)
315321
return color_list
316322

317323

318324
if __name__ == '__main__':
319-
image_color = ImageColor(url='https://s4.taihuoniao.com/opalus/image/190102/5c2c5a60ce156a0a1c0221e7')
320-
image_color.subject_detection()
321-
image_color.read_image()
322-
image_color.tailoring()
325+
image_color = ImageColor(url='https://s4.taihuoniao.com/opalus/image/191204/5de727835ba06347902cd72f')
323326
print(image_color.color_recognition())

0 commit comments

Comments
 (0)