我试图在检测到某个对象类(类=1--即 "人")时,让步进电机运行8.5圈(3400步)。
我使用Raspberry Pi和L298N与42BYG步进电机接口。
提前道歉,我的Python知识水平大约为0,我唯一的编程技能可以追溯到20世纪80年代末的Commodore 64 BASIC;)。
我让Tensorflow lite运行,并设法重新训练模型并使用以下代码。
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import io
import re
import time
from annotation import Annotator
import numpy as np
import picamera
from PIL import Image
from tflite_runtime.interpreter import Interpreter
CAMERA_WIDTH = 640
CAMERA_HEIGHT = 480
def load_labels(path):
"""Loads the labels file. Supports files with or without index numbers."""
with open(path, 'r', encoding='utf-8') as f:
lines = f.readlines()
labels = {}
for row_number, content in enumerate(lines):
pair = re.split(r'[:\s]+', content.strip(), maxsplit=1)
if len(pair) == 2 and pair[0].strip().isdigit():
labels[int(pair[0])] = pair[1].strip()
else:
labels[row_number] = pair[0].strip()
return labels
def set_input_tensor(interpreter, image):
"""Sets the input tensor."""
tensor_index = interpreter.get_input_details()[0]['index']
input_tensor = interpreter.tensor(tensor_index)()[0]
input_tensor[:, :] = image
def get_output_tensor(interpreter, index):
"""Returns the output tensor at the given index."""
output_details = interpreter.get_output_details()[index]
tensor = np.squeeze(interpreter.get_tensor(output_details['index']))
return tensor
def detect_objects(interpreter, image, threshold):
"""Returns a list of detection results, each a dictionary of object info."""
set_input_tensor(interpreter, image)
interpreter.invoke()
# Get all output details
boxes = get_output_tensor(interpreter, 0)
classes = get_output_tensor(interpreter, 1)
scores = get_output_tensor(interpreter, 2)
count = int(get_output_tensor(interpreter, 3))
results = []
for i in range(count):
if scores[i] >= threshold:
result = {
'bounding_box': boxes[i],
'class_id': classes[i],
'score': scores[i]
}
results.append(result)
return results
def annotate_objects(annotator, results, labels):
"""Draws the bounding box and label for each object in the results."""
for obj in results:
# Convert the bounding box figures from relative coordinates
# to absolute coordinates based on the original resolution
ymin, xmin, ymax, xmax = obj['bounding_box']
xmin = int(xmin * CAMERA_WIDTH)
xmax = int(xmax * CAMERA_WIDTH)
ymin = int(ymin * CAMERA_HEIGHT)
ymax = int(ymax * CAMERA_HEIGHT)
# Overlay the box, label, and score on the camera preview
annotator.bounding_box([xmin, ymin, xmax, ymax])
annotator.text([xmin, ymin],
'%s\n%.2f' % (labels[obj['class_id']], obj['score']))
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'--model', help='File path of .tflite file.', required=True)
parser.add_argument(
'--labels', help='File path of labels file.', required=True)
parser.add_argument(
'--threshold',
help='Score threshold for detected objects.',
required=False,
type=float,
default=0.4)
args = parser.parse_args()
labels = load_labels(args.labels)
interpreter = Interpreter(args.model)
interpreter.allocate_tensors()
_, input_height, input_width, _ = interpreter.get_input_details()[0]['shape']
with picamera.PiCamera(
resolution=(CAMERA_WIDTH, CAMERA_HEIGHT), framerate=30) as camera:
camera.start_preview()
try:
stream = io.BytesIO()
annotator = Annotator(camera)
for _ in camera.capture_continuous(
stream, format='jpeg', use_video_port=True):
stream.seek(0)
image = Image.open(stream).convert('RGB').resize(
(input_width, input_height), Image.ANTIALIAS)
start_time = time.monotonic()
results = detect_objects(interpreter, image, args.threshold)
elapsed_ms = (time.monotonic() - start_time) * 1000
annotator.clear()
annotate_objects(annotator, results, labels)
annotator.text([5, 0], '%.1fms' % (elapsed_ms))
annotator.update()
stream.seek(0)
stream.truncate()
finally:
camera.stop_preview()
if __name__ == '__main__':
main()
我还让步进电机正常运行 并使用这个脚本。
import RPi.GPIO as GPIO
import time
out1 = 13
out2 = 11
out3 = 15
out4 = 12
i=0
positive=0
negative=0
y=0
GPIO.setmode(GPIO.BOARD)
GPIO.setup(out1,GPIO.OUT)
GPIO.setup(out2,GPIO.OUT)
GPIO.setup(out3,GPIO.OUT)
GPIO.setup(out4,GPIO.OUT)
try:
while(1):
GPIO.output(out1,GPIO.LOW)
GPIO.output(out2,GPIO.LOW)
GPIO.output(out3,GPIO.LOW)
GPIO.output(out4,GPIO.LOW)
x = input()
x = int(x)
if x>0 and x<=400:
for y in range(x,0,-1):
if negative==1:
if i==7:
i=0
else:
i=i+1
y=y+2
negative=0
positive=1
#print((x+1)-y)
if i==0:
GPIO.output(out1,GPIO.HIGH)
GPIO.output(out2,GPIO.LOW)
GPIO.output(out3,GPIO.LOW)
GPIO.output(out4,GPIO.LOW)
time.sleep(0.001)
#time.sleep(1)
elif i==1:
GPIO.output(out1,GPIO.HIGH)
GPIO.output(out2,GPIO.HIGH)
GPIO.output(out3,GPIO.LOW)
GPIO.output(out4,GPIO.LOW)
time.sleep(0.001)
#time.sleep(1)
elif i==2:
GPIO.output(out1,GPIO.LOW)
GPIO.output(out2,GPIO.HIGH)
GPIO.output(out3,GPIO.LOW)
GPIO.output(out4,GPIO.LOW)
time.sleep(0.001)
#time.sleep(1)
elif i==3:
GPIO.output(out1,GPIO.LOW)
GPIO.output(out2,GPIO.HIGH)
GPIO.output(out3,GPIO.HIGH)
GPIO.output(out4,GPIO.LOW)
time.sleep(0.001)
#time.sleep(1)
elif i==4:
GPIO.output(out1,GPIO.LOW)
GPIO.output(out2,GPIO.LOW)
GPIO.output(out3,GPIO.HIGH)
GPIO.output(out4,GPIO.LOW)
time.sleep(0.001)
#time.sleep(1)
elif i==5:
GPIO.output(out1,GPIO.LOW)
GPIO.output(out2,GPIO.LOW)
GPIO.output(out3,GPIO.HIGH)
GPIO.output(out4,GPIO.HIGH)
time.sleep(0.001)
#time.sleep(1)
elif i==6:
GPIO.output(out1,GPIO.LOW)
GPIO.output(out2,GPIO.LOW)
GPIO.output(out3,GPIO.LOW)
GPIO.output(out4,GPIO.HIGH)
time.sleep(0.001)
#time.sleep(1)
elif i==7:
GPIO.output(out1,GPIO.HIGH)
GPIO.output(out2,GPIO.LOW)
GPIO.output(out3,GPIO.LOW)
GPIO.output(out4,GPIO.HIGH)
time.sleep(0.001)
#time.sleep(1)
if i==7:
i=0
continue
i=i+1
elif x<0 and x>=-400:
x=x*-1
for y in range(x,0,-1):
if positive==1:
if i==0:
i=7
else:
i=i-1
y=y+3
positive=0
negative=1
#print((x+1)-y)
if i==0:
GPIO.output(out1,GPIO.HIGH)
GPIO.output(out2,GPIO.LOW)
GPIO.output(out3,GPIO.LOW)
GPIO.output(out4,GPIO.LOW)
time.sleep(0.001)
#time.sleep(1)
elif i==1:
GPIO.output(out1,GPIO.HIGH)
GPIO.output(out2,GPIO.HIGH)
GPIO.output(out3,GPIO.LOW)
GPIO.output(out4,GPIO.LOW)
time.sleep(0.001)
#time.sleep(1)
elif i==2:
GPIO.output(out1,GPIO.LOW)
GPIO.output(out2,GPIO.HIGH)
GPIO.output(out3,GPIO.LOW)
GPIO.output(out4,GPIO.LOW)
time.sleep(0.001)
#time.sleep(1)
elif i==3:
GPIO.output(out1,GPIO.LOW)
GPIO.output(out2,GPIO.HIGH)
GPIO.output(out3,GPIO.HIGH)
GPIO.output(out4,GPIO.LOW)
time.sleep(0.001)
#time.sleep(1)
elif i==4:
GPIO.output(out1,GPIO.LOW)
GPIO.output(out2,GPIO.LOW)
GPIO.output(out3,GPIO.HIGH)
GPIO.output(out4,GPIO.LOW)
time.sleep(0.001)
#time.sleep(1)
elif i==5:
GPIO.output(out1,GPIO.LOW)
GPIO.output(out2,GPIO.LOW)
GPIO.output(out3,GPIO.HIGH)
GPIO.output(out4,GPIO.HIGH)
time.sleep(0.001)
#time.sleep(1)
elif i==6:
GPIO.output(out1,GPIO.LOW)
GPIO.output(out2,GPIO.LOW)
GPIO.output(out3,GPIO.LOW)
GPIO.output(out4,GPIO.HIGH)
time.sleep(0.001)
#time.sleep(1)
elif i==7:
GPIO.output(out1,GPIO.HIGH)
GPIO.output(out2,GPIO.LOW)
GPIO.output(out3,GPIO.LOW)
GPIO.output(out4,GPIO.HIGH)
time.sleep(0.001)
#time.sleep(1)
if i==0:
i=7
continue
i=i-1
except KeyboardInterrupt:
GPIO.cleanup()
有没有任何简单的方法来采取对象类,并告诉,如果类1被检测到,电机应该转指定的匝数?
我试过一些if参数,但估计我的Python知识还达不到解决这个问题的要求。
你应该尝试把步进电机代码做成一个函数。
def stepper_motor_turn(degrees, direction):
....
然后在检测器代码中应该是这样的
results = detect_objects(interpreter, image, args.threshold)
if <'results is equal to class 1'> :
stepper_motor_turn(...., ....)
当然,这只是一些伪代码,但看到你已经生成的代码,你应该能够得到类似这样的工作。
结果是一个由多个单一结果值组成的列表(一种python的 "数组")。你可以用'for'来迭代这些结果。每一个结果都是为每个找到的类id编写的一个字典。字典中的值你可以用它们的键名来访问。
results = [{'bounding_box': array([0.23692542, 0.17234364, 0.7129105 , 0.49806836], dtype=float32), 'class_id': 71.0, 'score': 0.4140625},
{'bounding_box': array([0.03008232, 0.04455566, 0.97868216, 0.8292703 ], dtype=float32), 'class_id': 81.0, 'score': 0.40234375}]
wanted_class_id = 71.0
wanted_minimal_score = 0.41 # or max ???
......
......
......
for result in results:
if result['class_id'] == wanted_class_id:
if result['score'] >= wanted_minimal_score:
# turn motor