{"id":67469,"date":"2023-10-21T11:08:36","date_gmt":"2023-10-21T02:08:36","guid":{"rendered":"https:\/\/mechsys.tec.u-ryukyu.ac.jp\/~oshiro\/SiteList\/?p=67469"},"modified":"2023-12-09T11:28:24","modified_gmt":"2023-12-09T02:28:24","slug":"google-colaboratory%e4%b8%8a%e3%81%aepython%e3%81%8b%e3%82%89%e3%81%ae%e3%82%ab%e3%83%a1%e3%83%a9%e5%88%a9%e7%94%a8","status":"publish","type":"post","link":"http:\/\/mechsys.tec.u-ryukyu.ac.jp\/~oshiro\/SiteList\/2023\/10\/21\/67469\/","title":{"rendered":"Google Colaboratory\u4e0a\u306ePython\u304b\u3089\u306e\u30ab\u30e1\u30e9\u5229\u7528"},"content":{"rendered":"<p>Python\u3067\u306e\u30ab\u30e1\u30e9\u4f7f\u7528\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u306fWindows\u306e\u30b9\u30bf\u30f3\u30c9\u30a2\u30ed\u30fc\u30f3\u74b0\u5883\u3067\u52d5\u304b\u3059\u5fc5\u8981\u304c\u3042\u308b\u3068\u8a71\u3057\u3066\u3044\u307e\u3057\u305f\u304c\u3001\u6539\u3081\u3066\u8abf\u3079\u308b\u3068Google Colaboratory\u4e0a\u3067\u3082\u52d5\u4f5c\u3059\u308b\u3053\u3068\u304c\u5206\u304b\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>Google Colab (https:\/\/colab.research.google.com\/ \uff09\u304b\u3089\u30ce\u30fc\u30c8\u30d6\u30c3\u30af\u3092\u65b0\u898f\u4f5c\u6210\u3067\u958b\u3044\u3066\u3001\u5de6\u7aef\u306e\u30e1\u30cb\u30e5\u30fc\u30a2\u30a4\u30b3\u30f3\u306e\u4e0b\u304b\u3089\u300c<>\u300d\u3068\u306a\u3063\u3066\u3044\u308b\u30b9\u30cb\u30da\u30c3\u30c8\u3092\u9078\u3073\u307e\u3059\u3002\u4eca\u5ea6\u306f\u3001\u53f3\u7aef\u306b\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u51e6\u7406\u306e\u3072\u306a\u578b\uff08\u30b9\u30cb\u30da\u30c3\u30c8\uff09\u306e\u4e00\u89a7\u304c\u51fa\u3066\u304f\u308b\u306e\u3067\u3001\u3053\u3053\u304b\u3089\u300cCamera Capture\u300d\u306e\u30b9\u30cb\u30da\u30c3\u30c8\u3092\u9078\u3093\u3067\u300c\u633f\u5165\u300d\u3092\u62bc\u3059\u3068\u3001Google Colab\u4e0a\u306b\u30bb\u30eb\u304c\uff12\u500b\u633f\u5165\u3055\u308c\u3066\u3001\u59cb\u3081\u306e\u30bb\u30eb\u3067 take_photo() \u3068\u3044\u3046\u95a2\u6570\u3092\u8a2d\u5b9a\u3057\u3066\u3001\u305d\u306e\u6b21\u306e\u30bb\u30eb\u3067\u5b9f\u969b\u306btake_photo()\u3092\u4f7f\u3063\u3066\u30ab\u30e1\u30e9\u753b\u50cf\u3092\uff11\u679a\u64ae\u5f71\u3057\u3066\u30d5\u30a1\u30a4\u30eb\u306b\u4fdd\u5b58\u3059\u308b\u51e6\u7406\u304c\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002\uff08\u8a66\u3059\u3060\u3051\u306a\u3089\u3059\u3050\u306b\u4f5c\u696d\u3067\u304d\u308b\u306e\u3067\u3001\u3082\u3057\u6642\u9593\u304c\u3042\u308c\u3070\u52d5\u4f5c\u3092\u78ba\u8a8d\u3057\u3066\u307f\u3066\u4e0b\u3055\u3044\u3002\uff09<\/p>\n<p>\u3053\u306e\u51e6\u7406\u306etake_photo()\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<div class=\"hl-surround\" style=\"height:280px;\"><div class=\"hl-main\"><pre>def take_photo(filename='photo.jpg', quality=0.8):\r\n  js = Javascript('''\r\n    async function takePhoto(quality) {\r\n      const div = document.createElement('div');\r\n      const capture = document.createElement('button');\r\n      capture.textContent = 'Capture';\r\n      div.appendChild(capture);\r\n\r\n      const video = document.createElement('video');\r\n      video.style.display = 'block';\r\n      const stream = await navigator.mediaDevices.getUserMedia({video: true});\r\n\r\n      document.body.appendChild(div);\r\n      div.appendChild(video);\r\n      video.srcObject = stream;\r\n      await video.play();\r\n\r\n      \/\/ Resize the output to fit the video element.\r\n      google.colab.output.setIframeHeight(document.documentElement.scrollHeight, true);\r\n\r\n      \/\/ Wait for Capture to be clicked.\r\n      await new Promise((resolve) =&gt; capture.onclick = resolve);\r\n\r\n      const canvas = document.createElement('canvas');\r\n      canvas.width = video.videoWidth;\r\n      canvas.height = video.videoHeight;\r\n      canvas.getContext('2d').drawImage(video, 0, 0);\r\n      stream.getVideoTracks()[0].stop();\r\n      div.remove();\r\n      return canvas.toDataURL('image\/jpeg', quality);\r\n    }\r\n    ''')\r\n  display(js)\r\n  data = eval_js('takePhoto({})'.format(quality))\r\n  binary = b64decode(data.split(',')[1])\r\n  with open(filename, 'wb') as f:\r\n    f.write(binary)\r\n  return filename\r\n}<\/pre><\/div><\/div>\n<p>\u3053\u308c\u306fPython\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u306a\u306e\u3067\u3059\u304c\u3001\u300c<code>js = Javascript(...)<\/code>\u300d\u3068\u306a\u3063\u3066\u3001\u30ab\u30e1\u30e9\u51e6\u7406\u306e\u5b9f\u4f53\u306f\u30a6\u30a7\u30d6\u30d6\u30e9\u30a6\u30b6\u5074\u306eJavaScript\u3067\u51e6\u7406\u3055\u308c\u3066\u3044\u307e\u3057\u305f\u3002<br \/>\n<center><br \/>\n<a href=\"http:\/\/mechsys.tec.u-ryukyu.ac.jp\/~oshiro\/SiteList\/wp-content\/uploads\/2023\/10\/59aa9c65ed25813e5b77e0b6eb336b21.gif\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/mechsys.tec.u-ryukyu.ac.jp\/~oshiro\/SiteList\/wp-content\/uploads\/2023\/10\/59aa9c65ed25813e5b77e0b6eb336b21.gif\" alt=\"\" width=\"521\" height=\"700\" class=\"aligncenter size-full wp-image-67476\" \/><\/a><br \/>\n<\/center><br \/>\n\u305d\u308c\u306f\u3068\u3082\u304b\u304f\u3001\u3053\u308c\u3067Google Colab\u3067\u30ab\u30e1\u30e9\u304c\u4f7f\u7528\u3067\u304d\u305d\u3046\u306a\u306e\u3067\u3001Google\u5074\u306eGPU\u3092\u4f7f\u3063\u305f\u51e6\u7406\u3082\u691c\u8a0e\u3067\u304d\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\n<p>\u4e0a\u8a18\u306e\u30b9\u30cb\u30da\u30c3\u30c8\u306f\uff11\u679a\u753b\u50cf\u3092\u64ae\u5f71\u3059\u308b\u3060\u3051\u3067\u3059\u304c\u3001\u9023\u7d9a\u7684\u306b\u51e6\u7406\u3059\u308b\u3053\u3068\u3082\u53ef\u80fd\u3067\u3059\uff08\u305f\u3060\u3057\u3001\u5f53\u7136\u306a\u304c\u3089JavaScript\u3082\u305d\u3053\u305d\u3053\u5206\u304b\u3063\u3066\u3044\u306a\u3044\u3068\u4f5c\u696d\u3067\u304d\u307e\u305b\u3093\uff09\u3002<\/p>\n<p>\u53c2\u8003\uff09<\/p>\n<ol>\n<li><a href=\"https:\/\/www.kkaneko.jp\/pro\/colab\/colabwebcam.html\">Google Colaboratory \u3067\u624b\u5143\u306e\u30ab\u30e1\u30e9\u3092\u4f7f\u3046\uff08Google Colaboratory \u63d0\u4f9b\u306e\u30b9\u30cb\u30da\u30c3\u30c8\u3092\u4f7f\u7528\uff09\u798f\u5c71\u5927\u91d1\u5b50\u7814<\/a><\/li>\n<li><a href=\"https:\/\/monomonotech.jp\/kurage\/memo\/m220126_colaboratory_webcamera.html\">colaboratory\u4e0a\u3067WEB\u30ab\u30e1\u30e9\u3092\u4f7f\u7528\u3059\u308b<\/a><\/li>\n<li><a href=\"https:\/\/monomonotech.jp\/kurage\/memo\/m230201_colab_webcamera_loop.html\">Colaboratory\u3067\u3001\u30eb\u30fc\u30d7\u51e6\u7406\u5185\u306e\u30ab\u30e1\u30e9\u6620\u50cf\u3092\u4f7f\u7528\u3059\u308b\u65b9\u6cd5<\/a><\/li>\n<li><a href=\"https:\/\/qiita.com\/Reishin\/items\/2b49ee70106d6c0c1276\">Google Colaboratory\u3067\u5916\u4ed8\u3051\u30ab\u30e1\u30e9\u3092\u4f7f\u3046 #Python &#8211; Qiita<\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Python\u3067\u306e\u30ab\u30e1\u30e9\u4f7f\u7528\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u306fWindows\u306e\u30b9\u30bf\u30f3\u30c9\u30a2\u30ed\u30fc\u30f3\u74b0\u5883\u3067\u52d5\u304b\u3059\u5fc5\u8981\u304c\u3042\u308b\u3068\u8a71\u3057\u3066\u3044\u307e\u3057\u305f\u304c\u3001\u6539\u3081\u3066\u8abf\u3079\u308b\u3068Google Colaboratory\u4e0a\u3067\u3082\u52d5\u4f5c\u3059\u308b\u3053\u3068\u304c\u5206\u304b\u308a\u307e\u3057\u305f\u3002 Google Co [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[468,41,433],"_links":{"self":[{"href":"http:\/\/mechsys.tec.u-ryukyu.ac.jp\/~oshiro\/SiteList\/wp-json\/wp\/v2\/posts\/67469"}],"collection":[{"href":"http:\/\/mechsys.tec.u-ryukyu.ac.jp\/~oshiro\/SiteList\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/mechsys.tec.u-ryukyu.ac.jp\/~oshiro\/SiteList\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/mechsys.tec.u-ryukyu.ac.jp\/~oshiro\/SiteList\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/mechsys.tec.u-ryukyu.ac.jp\/~oshiro\/SiteList\/wp-json\/wp\/v2\/comments?post=67469"}],"version-history":[{"count":6,"href":"http:\/\/mechsys.tec.u-ryukyu.ac.jp\/~oshiro\/SiteList\/wp-json\/wp\/v2\/posts\/67469\/revisions"}],"predecessor-version":[{"id":67477,"href":"http:\/\/mechsys.tec.u-ryukyu.ac.jp\/~oshiro\/SiteList\/wp-json\/wp\/v2\/posts\/67469\/revisions\/67477"}],"wp:attachment":[{"href":"http:\/\/mechsys.tec.u-ryukyu.ac.jp\/~oshiro\/SiteList\/wp-json\/wp\/v2\/media?parent=67469"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/mechsys.tec.u-ryukyu.ac.jp\/~oshiro\/SiteList\/wp-json\/wp\/v2\/categories?post=67469"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/mechsys.tec.u-ryukyu.ac.jp\/~oshiro\/SiteList\/wp-json\/wp\/v2\/tags?post=67469"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}