Last active
April 29, 2022 10:51
-
-
Save shikarunochi/900eac7876a0995512c6b2ed50f930a2 to your computer and use it in GitHub Desktop.
This file contains 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
#read punched tape | |
#MaixPy参考にしたサイト | |
#https://qiita.com/Lathe/items/7aeea7beaf05dbf8a539 | |
#https://qiita.com/Lathe/items/0f0c9b75928d52a7d502 | |
import sensor, image, time, lcd | |
lcd.init(freq=15000000) | |
sensor.reset() | |
sensor.set_pixformat(sensor.GRAYSCALE) # Set pixel format GRAYSCALE | |
sensor.set_framesize(sensor.VGA) # Set frame size to VGA (640x480) | |
sensor.set_contrast(2) | |
sensor.set_vflip(True); | |
sensor.skip_frames(time = 1000) # Wait for settings take effect. | |
clock = time.clock() # Create a clock object to track the FPS. | |
width = int(sensor.width()) | |
halfWidth = int(sensor.width() / 2) | |
height = sensor.height() | |
readFlag = False | |
#テープ位置検出用の判定 | |
frameBlackValue = 20 #これを下回ればガイドの黒枠 | |
frameWhiteValue = 80 #これを上回れば紙 | |
#テープ上での判定 | |
tapeBlackValue = 100 #これを下回れば黒(穴) | |
tapeWhiteValue = 150 #これを上回れば紙(穴なし) | |
while(True): | |
clock.tick() # Update the FPS clock. | |
img = sensor.snapshot() # Take a picture and return the image. | |
#img.binary([(0,100)]) | |
#img.invert() | |
topY = -1; | |
bottomY = -1; | |
topCheckY = -1; | |
bottomCheckY = -1; | |
#color = img.get_pixel(halfWidth-100,10) | |
#img.draw_circle(halfWidth-100,10,5,255) | |
#img.draw_string(halfWidth - 200,5,str(color),255,3) | |
#Y方向上から、黒色→白色が出るまで読み飛ばし。 | |
for y in range(1,int(height / 2)): | |
color = img.get_pixel(halfWidth,y) | |
if color < frameBlackValue: | |
topCheckY = y | |
break | |
if topCheckY > 0: | |
for y in range(topCheckY,int(height / 2)): | |
color = img.get_pixel(halfWidth,y) | |
if color > frameWhiteValue: | |
topY = y | |
break | |
#Y方向下から、黒色→白色が出るまで読み飛ばし。 | |
for y in reversed(range(int(height / 2),height)): | |
color = img.get_pixel(halfWidth,y) | |
if color < frameBlackValue: | |
bottomCheckY = y | |
break | |
if bottomCheckY > 0: | |
for y in reversed(range(int(height / 2),bottomCheckY)): | |
color = img.get_pixel(halfWidth,y) | |
if color > frameWhiteValue: | |
bottomY = y | |
break | |
if topY > 0 and bottomY > 0: #テープ発見 | |
img.draw_line(halfWidth -5,topY ,halfWidth - 5,bottomY,255,3 ) | |
tapeWidth = bottomY - topY #これがテープ幅 = 1インチ | |
#穴と穴の間隔は0.1インチ = tapeWidth *0.1 | |
#真ん中より一つ上のところが、紙送りチェック穴 | |
checkHoleY = topY + int((tapeWidth / 2 - tapeWidth * 0.1)) | |
#白→黒になった時に読み取る | |
color = img.get_pixel(halfWidth,checkHoleY) | |
img.draw_circle(halfWidth,checkHoleY, 10,255) | |
if readFlag == False and (color < tapeBlackValue): | |
#img.draw_circle(halfWidth,checkHoleY,4,255 ) | |
#読む | |
byte = 0 | |
holeTopY = topY + int(tapeWidth / 2 - tapeWidth * 0.1 * 4) | |
for holeIndex in range(0,9): | |
checkHoleY = int(holeTopY + tapeWidth * 0.1 * holeIndex) | |
color = img.get_pixel(halfWidth,checkHoleY) | |
img.draw_circle(halfWidth,checkHoleY, 10,255) | |
if color != None: | |
if holeIndex != 3: #紙送りホールは飛ばす | |
if color < tapeBlackValue: | |
img.draw_circle(halfWidth,checkHoleY,5,255,fill=True) | |
if holeIndex < 3: | |
byte = byte + (1 << holeIndex) | |
else: | |
byte = byte + (1 << (holeIndex - 1)) | |
img.draw_string(halfWidth - 200,5,str(byte) + ":" + chr(byte),255,3) | |
readFlag == True | |
elif readFlag == True and color > tapeWhiteValue: | |
readFlag = False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment