2
2
import cv2
3
3
import math
4
4
import numpy as np
5
- import base64
6
5
import requests
7
- from io import BytesIO
6
+ import time
8
7
9
8
10
9
def get_hue (a , b ):
@@ -118,9 +117,9 @@ def rgb2hex(rgb):
118
117
return color
119
118
120
119
121
- APP_ID = '15180965 '
122
- API_KEY = 'Gjw3RzhDcMSS8RESUEiVNWkH '
123
- SECRET_KEY = 'pZVtDe5Z74cA2ropdsI3s3rGMrFmXH9N '
120
+ APP_ID = '17887227 '
121
+ API_KEY = 'HXkluVYkuRL1PURlsa973vsl '
122
+ SECRET_KEY = 'khKaBZEkN2EnDoeRkU18pGbBF71CH2Fd '
124
123
125
124
126
125
class ImageColor :
@@ -130,19 +129,16 @@ def __init__(self, url=None, file_path=None):
130
129
self .param = None
131
130
self .url = url
132
131
self .img = None
132
+ self .response = None
133
133
134
134
# 读取图片
135
135
def read_image (self ):
136
136
if self .file_path :
137
137
self .img = cv2 .imread (self .file_path )
138
138
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 )
146
142
self .img = img
147
143
148
144
# 主体检测
@@ -156,22 +152,32 @@ def subject_detection(self):
156
152
self .param = response ['result' ]
157
153
elif self .url :
158
154
# 调用图像主体检测
159
- response = requests .get (self .url )
160
155
options = dict ()
161
156
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' ]
164
160
165
161
# 主体参数裁剪图片
166
162
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' ]]
169
166
# cv2.imshow('ai', initial_image) # 显示图片
170
167
# cv2.waitKey(0)
171
- self .img = initial_image
172
168
173
169
# 颜色识别
174
170
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)
175
181
height , width = self .img .shape [:2 ]
176
182
177
183
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):
180
186
image = cv2 .resize (self .img ,
181
187
(int (width / factor ), int (height / factor )),
182
188
interpolation = cv2 .INTER_LINEAR )
183
- cv2 .imshow ('ai' , image )
184
- cv2 .waitKey (0 )
185
189
LAB_image = cv2 .cvtColor (image , cv2 .COLOR_BGR2Lab )
186
190
frame_width = int (expected_size / 10 + 2 )
187
191
in_samples = []
@@ -311,13 +315,12 @@ def color_recognition(self, expected_size=40, in_clusters=7, out_clusters=3):
311
315
color = ',' .join ('%s' % id for id in color )
312
316
color_weight = round (proportions [i ] / sum (proportions ), 2 )
313
317
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)
315
321
return color_list
316
322
317
323
318
324
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' )
323
326
print (image_color .color_recognition ())
0 commit comments