我正试图将红色和绿色的值拉入firebase。如果我分别运行程序(红色和绿色),它会工作,但是如果我同时运行程序,它们都不工作,就会弹出以下json错误
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type int32 is not JSON serializable
这是我的密码:
import numpy as np
import cv2
from firebase import firebase
import time
import json
red_lower = np.array([-10, 100, 100])
red_upper = np.array([10, 255, 255]) # 这里是设置颜色
green_lower = np.array([35, 43, 46])
green_upper = np.array([77, 255, 255])
cap = cv2.VideoCapture(0)
cap.set(3, 320)
cap.set(4, 240)
def image_true_area(img):
return np.count_nonzero(img)
out_number = -1
thresh = 200
while 1:
ret, frame = cap.read()
frame = cv2.GaussianBlur(frame, (3, 3), 0)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
red_mask = cv2.inRange(hsv, red_lower, red_upper)
green_mask = cv2.inRange(hsv, green_lower, green_upper)
out_number = np.argmax([image_true_area(green_mask), image_true_area(red_mask), thresh])
out_number = out_number if out_number != 2 else -1
url="https://autocar-3885c-default-rtdb.firebaseio.com/"
connect = firebase.FirebaseApplication(url,authentication=None)
connect.put('/test/',data={'color':out_number} ,name="green")
print('已傳送',out_number)
mask = cv2.bitwise_or(red_mask, green_mask)
# 图像学膨胀腐 eclipse
mask = cv2.erode(mask, None, iterations=2)
mask = cv2.GaussianBlur(mask, (3, 3), 0)
res = cv2.bitwise_and(frame, frame, mask=mask)
# 寻找轮廓并绘制轮廓
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
if len(cnts) > 0:
# 寻找面积最大的轮廓并画出其最小外接圆
cnt = max(cnts, key=cv2.contourArea)
(x, y), radius = cv2.minEnclosingCircle(cnt)
cv2.circle(frame, (int(x), int(y)), int(radius), (255, 0, 255), 2)
# 找到物体的位置坐标,获得颜色物体的位置,可以来控制小车的转向
# print(int(x), int(y))
else:
pass
#cv2.putText(frame, str(out_number), (30, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2, cv2.LINE_AA)
print(out_number)
time.sleep(0.5)
cv2.imshow('frame', frame)
cv2.imshow('mask', mask)
cv2.imshow('res', res)
if cv2.waitKey(5) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()
暂无答案!
目前还没有任何答案,快来回答吧!