Created
November 26, 2017 05:41
-
-
Save alfredplpl/9661d8a152e812ccef5bc3e19192c99b to your computer and use it in GitHub Desktop.
自然非言語処理第1日目のソースコード
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # coding: UTF-8 | |
| __auhtor__="alfredplpl" | |
| #See also: | |
| #https://westus.dev.cognitive.microsoft.com/docs/services/5639d931ca73072154c1ce89/operations/56f23eb019845524ec61c4d7 | |
| #http://labs.eecs.tottori-u.ac.jp/sd/Member/oyamada/OpenCV/html/py_tutorials/py_gui/py_video_display/py_video_display.html | |
| import httplib,cv2,json | |
| ## constants | |
| headers = { | |
| # Request headers | |
| 'Content-Type': 'application/octet-stream', | |
| 'Ocp-Apim-Subscription-Key': '[Your Key]', | |
| } | |
| SIZE=(480,270) | |
| WAIT_TIME=5000#[ms] | |
| HAPINESS_PATH="./img/hapiness.png" | |
| SADNESS_PATH="./img/sadness.png" | |
| NEUTRAL_PATH="./img/neutral.png" | |
| HAPINESS_IMG=cv2.imread(HAPINESS_PATH) | |
| SADNESS_IMG=cv2.imread(SADNESS_PATH) | |
| NEUTRAL_IMG=cv2.imread(NEUTRAL_PATH) | |
| ############ | |
| if __name__ == "__main__": | |
| cap = cv2.VideoCapture(0) | |
| while (True): | |
| ret, frame = cap.read() | |
| resized=cv2.resize(frame,SIZE) | |
| cv2.imshow("Captured",resized) | |
| cv2.imencode(".jpg",resized) | |
| result, encimg = cv2.imencode('.jpg', resized) | |
| try: | |
| conn = httplib.HTTPSConnection('westus.api.cognitive.microsoft.com') | |
| conn.request("POST", "/emotion/v1.0/recognize", encimg, | |
| headers) | |
| response = conn.getresponse() | |
| data = response.read() | |
| print(data) | |
| conn.close() | |
| except Exception as e: | |
| print("[Errno {0}] {1}".format(e.errno, e.strerror)) | |
| if cv2.waitKey(WAIT_TIME) & 0xFF == ord('q'): | |
| break | |
| result=json.loads(data) | |
| if(len(result)>0): | |
| if(result[0]["scores"]["happiness"]>0.5): | |
| cv2.imshow("AgentResponse",HAPINESS_IMG) | |
| elif (result[0]["scores"]["anger"] > 0.3): | |
| cv2.imshow("AgentResponse",SADNESS_IMG) | |
| else: | |
| cv2.imshow("AgentResponse", NEUTRAL_IMG) | |
| if cv2.waitKey(WAIT_TIME) & 0xFF == ord('q'): | |
| break | |
| cap.release() | |
| cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment