HackUTOKYOv2Pim

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

hacku-tokyo-v2-pim.py (3062B)


      1 #!/usr/bin/python
      2 # coding: utf-8
      3 import RPi.GPIO as GPIO
      4 import time
      5 import sys
      6 import cv2
      7 import os
      8 
      9 stepPin = 22
     10 dircPin = 17
     11 enabPin = 23
     12 
     13 camLimit = 20
     14 
     15 
     16 GPIO.setmode(GPIO.BCM)  # GPIOで指定
     17 GPIO.setup(enabPin, GPIO.OUT)   # 2:Enableに定義
     18 GPIO.setup(dircPin, GPIO.OUT)   # 3:Dir
     19 GPIO.setup(stepPin, GPIO.OUT)   # 4:Step
     20 
     21 #GPIO.setup(17, GPIO.OUT)#17:MS1
     22 #GPIO.setup(27, GPIO.OUT)#27:MS2
     23 #GPIO.setup(22, GPIO.OUT)#22:MS3
     24 
     25 def main():
     26     place = 0
     27     GPIO.output(enabPin, 0)
     28     for i in range(0,4):
     29         place = escapeR(place)
     30         print(place)
     31 
     32     while(True):
     33         ret, frame = cap.read()
     34 
     35         gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 
     36         mos = mosaic(gray)
     37         count = dump(gray, keep)
     38              
     39         print(count)
     40 
     41         if count[0] >= camLimit:
     42             if count[0] > count[1]:
     43                 print("R>L")
     44                 place = escapeR(place)
     45             else:
     46                 print("R<L")
     47                 place = escapeL(place)
     48         elif count[1] >= camLimit:
     49             place = escapeL(place)
     50         
     51         if cv2.waitKey(1) != -1:
     52             break
     53 
     54         print(place)
     55 
     56     GPIO.output(enabPin,1)
     57     GPIO.cleanup()
     58 
     59 #逆かもしれない
     60 def escapeR(place):
     61     if(place >= 7):
     62         print("over16")
     63         return place
     64     else:
     65         GPIO.output(dircPin, 1)
     66         for num in range(0,500):
     67             GPIO.output(stepPin, 1)
     68             time.sleep(0.001)
     69             GPIO.output(stepPin, 0)
     70             time.sleep(0.001)
     71         place += 1
     72     return place
     73 
     74 def escapeL(place):
     75     if(place <= 0):
     76         print("under0")
     77         return place
     78     else:
     79         GPIO.output(dircPin, 0)
     80         for num in range(0,500):
     81             GPIO.output(stepPin, 1)
     82             time.sleep(0.001)
     83             GPIO.output(stepPin, 0)
     84             time.sleep(0.001)
     85         place -= 1
     86     return place
     87 
     88 def mosaic(src):
     89     dst = cv2.resize(src, None, fx=0.1, fy=0.1, interpolation=cv2.INTER_NEAREST)
     90     return cv2.resize(dst, src.shape[:2][::-1], interpolation=cv2.INTER_NEAREST)
     91 
     92 def difference(a, b):
     93     if(a < b):
     94         return b - a
     95     return a - b
     96 
     97 def dump(src, keep):
     98     #os.system('clear')
     99     RightCount = 0
    100     LeftCount = 0
    101     for x in range(0, 32):
    102         str = ''
    103         for y in range(0, 24):
    104             target = src[y*10][x*10]    
    105             diff = difference(keep[y][x],target)
    106             if diff > 70:
    107                 str += '{:02} '.format(diff) 
    108                 if(x >= 16):
    109                      RightCount += 1
    110                 else:
    111                      LeftCount += 1
    112             else:
    113                 str += '-- '
    114             keep[y][x] = target
    115         #print(str)
    116     count=[RightCount,LeftCount]
    117     return count
    118 # 仕様メモcount=[RightCount,LeftCount]
    119 
    120 cap = cv2.VideoCapture(-1)
    121 
    122 cap.set(3,320) # WIDTH
    123 cap.set(4,240) # HEIGHT
    124 cap.set(5,30) # FPS
    125 
    126 keep = [[0] * 32 for i in range(24)]
    127 
    128 busy = True
    129 
    130 try:
    131     main()
    132 except KeyboardInterrupt:
    133     print('interrupted!')
    134     GPIO.output(enabPin,1)
    135     GPIO.cleanup()
    136 
    137 cap.release()
    138 cv2.destroyAllWindows()