我正在开发一个应用程序,该应用程序可读取摄像头信号并将帧与其他一些数据一起发送。我目前正在使用线程读取来自 8 个不同相机的提要。
问题是,当我将数据包发送到 Flask 服务器时,出现 ReadTimeout
错误。
直到几天前它才完美运行,但我突然开始遇到这个错误。我相信错误出在服务器端,但我无法弄清楚是不是它导致了这个错误。
服务器端代码:
app2 = Flask(__name__)
cors = CORS(app2)
@app2.route("/", methods=["POST"])
def index():
lock.acquire()
zone_data = {}
vehicle_data = {}
try:
start_time = time.time()
np_array = np.frombuffer(request.data, np.uint8)
frame = cv2.imdecode(np_array, cv2.IMREAD_COLOR)
id = int(request.headers["id"])
gw.cameras[id-1].last_receive_time = time.time()
gw.cameras[id-1].server_online = True
gw.cameras[id-1].online = True
gw.cameras[id-1].information = json.loads(request.headers["occupancy_information"])
gw.cameras[id-1].deque.append(frame)
if gw.cameras[id-1].zone_manager.is_changed:
zone_data = gw.cameras[id-1].zone_manager.all_zones
vehicle_data = gw.cameras[id-1].zone_manager.all_vehicles
response = {
'cam_id': str(gw.cameras[id-1].idx),
'is_changed': gw.cameras[id-1].zone_manager.is_changed,
'zone_data': zone_data,
'vehicle_data': vehicle_data
}
gw.cameras[id-1].zone_manager.is_changed = False
return Response(response=str(response), status=200, mimetype="application/json")
except Exception as e:
logging.info(e)
pass
lock.release()
POST 请求发送代码:
try:
headers = {
"id": str(self.instance_id),
"occupancy_information": json.dumps(self.occupancy_info)
}
_, img_encoded = cv2.imencode('.jpg', frame)
response = requests.post(
'http://{}:{}/'.format(self.target_ip, self.target_port + self.instance_id),
data=img_encoded.tostring(),
headers=headers,
timeout=10
)
resp = ast.literal_eval(response.content.decode('utf-8'))
if response.status_code != 200:
self.device_status = 0
if resp['is_changed']:
self.zone_data = resp['zone_data']
self.vehicle_data = resp['vehicle_data']
self.zone_data = dict(("C" + str(int(key[1])) + key[2:], value) for (key, value) in self.zone_data.items())
self.scene = np.zeros((1080, 1920))
for zone_id, zone in self.zone_data.items():
y = [coords[0] for coords in zone]
x = [coords[1] for coords in zone]
rr, cc = polygon(x, y)
self.scene[rr, cc] = int(zone_id[-1])
with open(self.zone_config_path, 'w') as file_reader:
json.dump({"zone_data":self.zone_data, "vehicle_data":self.vehicle_data}, file_reader)
except Exception as e:
self.event_logs.info("ERROR Instance-{}: {}".format(self.instance_id, e))
self.device_status = 0
print("UI",self.instance_id, e)
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno)
finally:
pass