【发布时间】:2021-04-28 04:47:45
【问题描述】:
我编写了一个代码来控制英雄联盟中由健康百分比变化引起的变化。我使用 python 和 OCR 来检测健康状况并将健康状况数字发送到 Arduino 来控制 LED,使其在健康状况 > 80% 时延迟 3 秒闪烁,当健康状况
问题在于,对于所有健康百分比,LED 只会延迟 3 秒闪烁。我该怎么办 ?提前谢谢
Python 代码:
import serial
import time
import numpy as np
import cv2
from PIL import ImageGrab, Image
import os
import pytesseract
ser = serial.Serial('COM3', 9600)
#This loop allows opencv to capture the screen continuously
while True:
img = ImageGrab.grab(bbox=[840, 1020, 940, 1050 ])
img_np = np.array(img)
frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)
text = pytesseract.image_to_string(frame)
v3=-1
if "/" in text:
x = text.split("/")
try:
v1 = int(x[0])
v2 = int(x[1])
v3 = int((v1/v2)*100)
if v3<=100:
print(str(v3))
v4= str(abs(v3))
ser.write(b'v4')
time.sleep(0.1)
time.sleep(2)
except:
pass
cv2.imshow("Screen", frame)
if cv2.waitKey(1) == 27:
break
cv2.destroyAllWindows()
Arduino 代码
const int led=8;
String value;
int i;
void setup()
{
Serial.begin(9600);
pinMode(led, OUTPUT);
Serial.println("Connection established...");
}
void loop()
{
while (Serial.available())
{
value = Serial.read();
i = value.toInt();
if (100 >= i > 80)
{
digitalWrite(led, HIGH);
delay(3000);
digitalWrite(led, LOW);
}
else if (0 <= i < 80)
{
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
}
else { digitalWrite(led, LOW);
}
}
}
在此处输入代码
【问题讨论】:
-
这不是调试服务。你应该自己缩小问题的范围。例如,您应该打印 Arduino 收到的值,以找出问题出在项目的 Arduino 方面还是 Python 方面……您不需要我们将您的项目分解为多个部分并检查每个部分他们。你得到正确的 OCR 结果吗,你发送了正确的值,你收到了正确的值。这就是你可以通过简单的印刷品找到的所有东西。您不需要我们的专业知识
-
感谢您的建议,我真的很感激。我确保 python 端没问题。但是如何监控 Arduino 输出?因为当我尝试使用串行监视器时,它给了我一个端口繁忙的错误,因为 python 使用该端口
-
要么将第二个串行接口连接到您的 arduino(ttl 到 USB 适配器或逻辑分析仪),或者更简单地让您的 Arduino 将每个接收到的字节发送回 python
标签: python arduino serial-port ocr tesseract