{"id":19830,"date":"2023-04-20T21:50:00","date_gmt":"2023-04-20T12:50:00","guid":{"rendered":""},"modified":"2024-09-16T01:26:34","modified_gmt":"2024-09-15T16:26:34","slug":"python-aiprograminng-2","status":"publish","type":"post","link":"http:\/\/www.tyosuke20xx.com\/blog\/?p=19830","title":{"rendered":"Python AIprograminng"},"content":{"rendered":"<p>import IPython<br \/>\nfrom google.colab import output<br \/>\nimport cv2<br \/>\nimport numpy as np<br \/>\nfrom PIL import Image<br \/>\nfrom io import BytesIO<br \/>\nimport base64<\/p>\n<p>def run(img_str):<br \/>\n  #decimg to image<br \/>\n  decimg = base64.b64decode(img_str.split(&#8216;,&#8217;)[1], validata=True)<br \/>\n  decimg = Image.open(BytesIO(decimg))<br \/>\n  decimg = np.array(decimg, dtype=np.uint8);<br \/>\n  decimg = cv2.cvtColor(decimg, cv2.COLOR_BGR2RGB)<\/p>\n<p>  # image classify<br \/>\n  img = cv2.resize(decimg, (64, 64))<br \/>\n  img = img_to_array(img)<br \/>\n  img = img \/ 255.0<br \/>\n  img = img[np.newaxis]<br \/>\n  img = np.asarray(img)<br \/>\n  preds = keras_model.predict(img)<br \/>\n  result = np.argmax(preds[0])<\/p>\n<p>  # draw output<br \/>\n  out_img = decimg<br \/>\n  cv2.putText(out_img, labels[result], (10,50),<br \/>\n      cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 1, cv2.LINE_AA)<br \/>\n  #encode to string<br \/>\n  _, encimg = cv2.imencode(&#8220;.jpg&#8221;, out_img,<br \/>\n      [int(cv2.IMWRITE_JPEG_QUALITY), 80])<br \/>\n  img_str = encimg.tostring()<br \/>\n  img_str = (&#8220;data:imge\/jpeg:base64,&#8221; +<br \/>\n      base64.b64encode(img_str).decode(&#8216;utf-8&#8217;))<br \/>\n  return IPython.display.JSON({&#8216;img_str&#8217;: img_str})<\/p>\n<p>output.register_callback(&#8216;notebook.run&#8217;,run)<\/p>\n<p>https:\/\/colab.research.google.com\/drive\/1cF&#8211;C6PRT2j7hPdn5ia1I7SlwGqdvB-d?hl=ja#scrollTo=eT8PXuywDsmO<\/p>\n","protected":false},"excerpt":{"rendered":"<p>import IPython from google.colab import output import cv2 import numpy as np from PIL import Image from io imp &hellip; <a href=\"http:\/\/www.tyosuke20xx.com\/blog\/?p=19830\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;Python AIprograminng&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[65],"tags":[],"class_list":["post-19830","post","type-post","status-publish","format-standard","hentry","category-python"],"aioseo_notices":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/19830","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=19830"}],"version-history":[{"count":1,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/19830\/revisions"}],"predecessor-version":[{"id":19982,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/19830\/revisions\/19982"}],"wp:attachment":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=19830"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=19830"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=19830"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}