# Hello World Example # # Welcome to the OpenMV IDE! Click on the green run arrow button below to run the script!
import sensor, image, time
sensor.reset() # Reset and initialize the sensor. sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE) sensor.set_framesize(sensor.QVGA) # Set frame size to QVGA (320x240) sensor.skip_frames(time = 2000) # Wait for settings take effect. clock = time.clock() # Create a clock object to track the FPS.
while(True): clock.tick() # Update the FPS clock. img = sensor.snapshot() # Take a picture and return the image. print(clock.fps()) # Note: OpenMV Cam runs about half as fast when connected # to the IDE. The FPS should increase once disconnected.
# QRCode Example # # This example shows the power of the OpenMV Cam to detect QR Codes # using lens correction (see the qrcodes_with_lens_corr.py script for higher performance).
import sensor, image, time
sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.skip_frames(time = 2000) sensor.set_auto_gain(False) # must turn this off to prevent image washout... clock = time.clock()
while(True): clock.tick() img = sensor.snapshot() img.lens_corr(1.8) # strength of 1.8 is good for the 2.8mm lens. for code in img.find_qrcodes(): img.draw_rectangle(code.rect(), color = (255, 0, 0)) print(code) print(clock.fps())
# Measure the distance # # This example shows off how to measure the distance through the size in imgage # This example in particular looks for yellow pingpong ball.
import sensor, image, time, lcd
# For color tracking to work really well you should ideally be in a very, very, # very, controlled enviroment where the lighting is constant... #yellow_threshold = (22, 74, -12, 31, 23, 62)
box_threshold = (40, 80, -4, 21, 14, 51)
red_threshold = (40, 59, 61, 84, 30, 79) # You may need to tweak the above settings for tracking green things... # Select an area in the Framebuffer to copy the color settings.
sensor.reset() # Initialize the camera sensor. sensor.set_pixformat(sensor.RGB565) # use RGB565. sensor.set_framesize(sensor.QVGA) # use QQVGA for speed. sensor.skip_frames(10) # Let new settings take affect. sensor.set_auto_whitebal(False) # turn this off. clock = time.clock() # Tracks FPS.
#lcd.init() # Initialize the lcd screen.
K=5000#the value should be measured
while(True): clock.tick() # Track elapsed milliseconds between snapshots(). img = sensor.snapshot() # Take a picture and return the image.
blobs = img.find_blobs([red_threshold]) if len(blobs) == 1: # Draw a rect around the blob. #b = blobs[0] #img.draw_rectangle(b[0:4]) # rect #img.draw_cross(b[5], b[6]) # cx, cy #Lm = (b[2]+b[3])/2 #length = K/Lm #print(length) b = blobs[0] img.draw_rectangle(b[0:4]) img.draw_cross(b[5], b[6]) #lcd.display(sensor.snapshot()) # Take a picture and display the image. #print(clock.fps()) # Note: Your OpenMV Cam runs about half as fast while # connected to your computer. The FPS should increase once disconnected.
for r in img.find_rects(threshold = 3500): img.draw_rectangle(r.rect(), color=(255,0,0)) for p in r.corners(): img.draw_circle(p[0], p[1], 5, color=(0,255,0)) print(r) print("FPS %f" % clock.fps())
# Measure the distance # # This example shows off how to measure the distance through the size in imgage # This example in particular looks for yellow pingpong ball.
import sensor, image, time
# For color tracking to work really well you should ideally be in a very, very, # very, controlled enviroment where the lighting is constant... yellow_threshold = ( 56, 83, 5, 57, 63, 80) # box_threshold = (40, 80, -4, 21, 14, 51) # red_threshold = (40, 59, 61, 84, 30, 79)
# You may need to tweak the above settings for tracking green things... # Select an area in the Framebuffer to copy the color settings.
sensor.reset() # Initialize the camera sensor. sensor.set_pixformat(sensor.RGB565) # use RGB565. sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed. sensor.skip_frames(10) # Let new settings take affect. sensor.set_auto_whitebal(False) # turn this off. clock = time.clock() # Tracks FPS.
K=5000#the value should be measured # K = 36*10
while(True): clock.tick() # Track elapsed milliseconds between snapshots(). img = sensor.snapshot() # Take a picture and return the image.
blobs = img.find_blobs([yellow_threshold]) if len(blobs) == 1: # Draw a rect around the blob. b = blobs[0] img.draw_rectangle(b[0:4]) # rect img.draw_cross(b[5], b[6]) # cx, cy Lm = (b[2]+b[3])/2# 像素点数 # index 2 is length, 3 is width length = K/Lm # 距离 print(length)
#print(clock.fps()) # Note: Your OpenMV Cam runs about half as fast while # connected to your computer. The FPS should increase once disconnected.
LCD显示
由于正好有个LCD显示屏,那就也记录下LCD的代码把
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# LCD Example # # Note: To run this example you will need a LCD Shield for your OpenMV Cam. # # The LCD Shield allows you to view your OpenMV Cam's frame buffer on the go.
import sensor, image, lcd
sensor.reset() # Initialize the camera sensor. sensor.set_pixformat(sensor.RGB565) # or sensor.GRAYSCALE sensor.set_framesize(sensor.QQVGA2) # Special 128x160 framesize for LCD Shield. lcd.init() # Initialize the lcd screen.
while(True): lcd.display(sensor.snapshot()) # Take a picture and display the image.
# UART Control # # This example shows how to use the serial port on your OpenMV Cam. Attach pin # P4 to the serial input of a serial LCD screen to see "Hello World!" printed # on the serial LCD display.
import time from pyb import UART
# Always pass UART 3 for the UART number for your OpenMV Cam. # The second argument is the UART baud rate. For a more advanced UART control # example see the BLE-Shield driver. uart = UART(3, 19200)