raspberrypi4 LEDの制御







今回は今まで次の表情認識を行うまで

ずっとライトがつきっぱなしであった

次の表情認識を行うまでライトを消すということを行う。



✳︎プログラム

  1. import cognitive_face as CF
  2. import json
  3. import picamera
  4. import time
  5. import cv2 as cv
  6. import pygame
  7. import time
  8. import RPi.GPIO as GPIO
  9. import time, sys
  10. import pygame
  11. import time
  12.  
  13. #ポート番号の指定
  14. RED = 17
  15. GREEN = 27
  16. BLUE = 22
  17.  
  18. #GPIOポートの初期化
  19. GPIO.setmode(GPIO.BCM)
  20. ports = [RED, GREEN, BLUE]
  21. for port in ports:
  22.     GPIO.setup(port, GPIO.OUT)
  23. KEY = ''
  24. ENDPOINT = ''
  25. CF.Key.set(KEY)
  26. CF.BaseUrl.set(ENDPOINT)
  27. while True:
  28.     with picamera.PiCamera() as camera:
  29.         camera.resolution = (512,384)
  30.         camera.capture('test.jpg')
  31.         print('capture')
  32.         img = cv.imread('test.jpg')
  33.         grayimg = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
  34.         img = "test.jpg"
  35.         faces = CF.face.detect(img, attributes='emotion')
  36.         f=json.dumps(faces)
  37.         j=json.loads(f)
  38.         for i in range(0,len(j)):
  39.             print(j[i]['faceAttributes']['emotion']['happiness'])
  40.             happiness_val = j[i]['faceAttributes']['emotion']['happiness']
  41.             
  42.             print(j[i]['faceAttributes']['emotion']['sadness'])
  43.             sadness_val = j[i]['faceAttributes']['emotion']['sadness']
  44.             
  45.             print(j[i]['faceAttributes']['emotion']['anger'])
  46.             anger_val = j[i]['faceAttributes']['emotion']['anger']
  47.             
  48.             
  49.             
  50.             
  51.             
  52.             
  53.             if happiness_val > 0.5 :
  54.                 
  55.                 GPIO.output(RED,0)
  56.                 GPIO.output(GREEN,1)
  57.                 GPIO.output(BLUE, 0)
  58.                 print("幸せ")
  59.                 
  60.                 
  61.                 pygame.mixer.init(frequency=44100)
  62.                 pygame.mixer.music.load("/home/pi/mypython/camera/audio/cat1a.mp3")
  63.                 pygame.mixer.music.play(1)
  64.                 time.sleep(5)
  65.                 pygame.mixer.music.stop()
  66.                 
  67.                 
  68.                 GPIO.output(RED,0)
  69.                 GPIO.output(GREEN,0)
  70.                 GPIO.output(BLUE, 0)
  71.                 
  72.                 
  73.                 
  74.             
  75.             elif sadness_val > 0.5 :
  76.                 
  77.                     GPIO.output(RED,0)
  78.                     GPIO.output(GREEN,0)
  79.                     GPIO.output(BLUE, 1)
  80.                     print("悲しい")
  81.                 
  82.                 
  83.                     pygame.mixer.init(frequency=44100)
  84.                     pygame.mixer.music.load("/home/pi/mypython/camera/audio/old_cat1.mp3")
  85.                     pygame.mixer.music.play(1)
  86.                     time.sleep(5)
  87.                     pygame.mixer.music.stop()
  88.                     
  89.                     
  90.                     
  91.                     GPIO.output(RED,0)
  92.                     GPIO.output(GREEN,0)
  93.                     GPIO.output(BLUE, 0)
  94.             
  95.                         
  96.             elif anger_val > 0.5 :
  97.                 
  98.                     GPIO.output(RED,1)
  99.                     GPIO.output(GREEN,0)
  100.                     GPIO.output(BLUE, 0)
  101.                     print("怒ってる")
  102.                 
  103.                 
  104.                     pygame.mixer.init(frequency=44100)
  105.                     pygame.mixer.music.load("/home/pi/mypython/camera/audio/purring.mp3")
  106.                     pygame.mixer.music.play(1)
  107.                     time.sleep(5)
  108.                     pygame.mixer.music.stop()
  109.                     
  110.                     
  111.                     GPIO.output(RED,0)
  112.                     GPIO.output(GREEN,0)
  113.                     GPIO.output(BLUE, 0)