Python AIprograminng

import IPython
from google.colab import output
import cv2
import numpy as np
from PIL import Image
from io import BytesIO
import base64

def run(img_str):
#decimg to image
decimg = base64.b64decode(img_str.split(‘,’)[1], validata=True)
decimg = Image.open(BytesIO(decimg))
decimg = np.array(decimg, dtype=np.uint8);
decimg = cv2.cvtColor(decimg, cv2.COLOR_BGR2RGB)

# image classify
img = cv2.resize(decimg, (64, 64))
img = img_to_array(img)
img = img / 255.0
img = img[np.newaxis]
img = np.asarray(img)
preds = keras_model.predict(img)
result = np.argmax(preds[0])

# draw output
out_img = decimg
cv2.putText(out_img, labels[result], (10,50),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 1, cv2.LINE_AA)
#encode to string
_, encimg = cv2.imencode(“.jpg”, out_img,
[int(cv2.IMWRITE_JPEG_QUALITY), 80])
img_str = encimg.tostring()
img_str = (“data:imge/jpeg:base64,” +
base64.b64encode(img_str).decode(‘utf-8’))
return IPython.display.JSON({‘img_str’: img_str})

output.register_callback(‘notebook.run’,run)

https://colab.research.google.com/drive/1cF–C6PRT2j7hPdn5ia1I7SlwGqdvB-d?hl=ja#scrollTo=eT8PXuywDsmO

投稿者: chosuke

趣味はゲームやアニメや漫画などです

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です