def predict(url):
global model, COOKED_PHRASES, RAW_PHRASES
# Read image
image = io.imread(url)
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
image = cv2.resize(image, (500, 500), interpolation=cv2.INTER_CUBIC)
# Use otsu to mask
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
mask = cv2.medianBlur(mask, 5)
# Get features
features = describe(image, mask)
# Predict it
result = model.predict([features])
probability = model.predict_proba([features])[0][result][0]
state = le.inverse_transform(result)[0]
phrase = ''
if 'cook' in state:
phrase = COOKED_PHRASES[int(random.random()*len(COOKED_PHRASES))]
elif 'raw' in state:
phrase = RAW_PHRASES[int(random.random()*len(RAW_PHRASES))]
return {'type': state, 'confidence': probability, 'phrase': phrase}
评论列表
文章目录