import cv2
import sys
import serial
import time
import numpy as np
from PyQt5.QtWidgets import QLabel,QApplication
from PyQt5.QtGui import QPixmap
green_val = 90
ser = serial.Serial("/dev/ttyAMA0",9600) # 位置1
ser.flushInput() # 位置2
#ser.write("begin".encode("utf-8")) # 位置3
#标签初始化
app = QApplication(sys.argv)
label = QLabel()
label.setPixmap(QPixmap("123.jpg"))
label.show()
#label.showFullScreen()
label.setScaledContents(True) # 让图片自适应label大
#cv2.waitKey(10000)
def shibie():
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
# get a frame
#frame = cv2.imread("home\pi\lanse.jpg")
cv2.imshow("haha",frame)
cv2.waitKey(2000)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
blue_lower = np.array([green_val - 10, 100, 100])
blue_upper = np.array([green_val + 10, 255, 255])
blue_mask = cv2.inRange(hsv, blue_lower, blue_upper)
blue_res = cv2.bitwise_and(frame, frame, mask=blue_mask)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
blue_closing = cv2.morphologyEx(blue_res, cv2.MORPH_CLOSE, kernel)
blue_gray = cv2.cvtColor(blue_closing, cv2.COLOR_BGR2GRAY)
(thresh2, blue_bw) = cv2.threshold(blue_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
blue_black = cv2.countNonZero(blue_bw)
cv2.imshow("hahaa",blue_bw)
cv2.waitKey(2000)
print(blue_black)
if blue_black > 121000:#判断二值化像素点个数
print("视频中有蓝色像素")
ser.write("1".encode("utf-8"))#如果没有颜色则发送1
#如果有蓝色像素则显示蓝色图片
label.setPixmap(QPixmap("lanse.jpg"))
label.show()
#label.showFullScreen()
label.setScaledContents(True) # 让图片自适应label大小
#if a==0xff:#如果有新的拍照指令则退出,继续循环
#break
else:
print("无蓝色")
ser.write("2".encode("utf-8"))#如果没有颜色则发送2
#没有蓝色则显示原始图像
label.setPixmap(QPixmap("123.jpg"))
label.show()
#label.showFullScreen()
label.setScaledContents(True) # 让图片自适应label大小
# 完成所有操作后,释放捕获
cap.release()
# show a frame
#cv2.imshow("capture", frame)
#cv2.imwrite("home\pi\lanse.jpg", frame)
#a = cv2.imread("home\pi\lanse.jpg")
#cv2.imshow("duqu",a)
if __name__ == "__main__":
while(True):
count = ser.inWaiting() # 位置4 读取返回值
print(count)
if count != 0:
recv = ser.read(count) # 位置5
#如果接受到01则开始识别
if recv == b"\x01":
print(recv)
shibie()
#ser.write("Recv some data is : ".encode("utf-8")) # 位置6
#ser.write(recv) # 位置7
ser.flushInput()
time.sleep(0.1) # 位置8
#shibie()
cv2.destroyAllWindows()