Commit dc14f2a5 authored by Liantao Wu's avatar Liantao Wu

Upload New File

parent 4d42d35f
import cv2
import json
from socket import *
from tool.utils import load_class_names, plot_boxes_cv2
import queue, _thread, threading, time
import numpy as np
import requests
'''
用摄像头跑两系统
可放弃
'''
ip_add = '192.168.1.118'
# ip_add = '127.0.0.1'
# ip_add = ''
def send_from(arr, dest):
view = memoryview(arr).cast('B')
while len(view):
nsent = dest.send(view)
view = view[nsent:]
def recv_into(arr, source):
view = memoryview(arr).cast('B')
while len(view):
nrecv = source.recv_into(view)
view = view[nrecv:]
s = socket(AF_INET, SOCK_STREAM)
s.bind(('', 25000))
s.listen(1)
c,a = s.accept()
print(1)
c_2 = socket(AF_INET, SOCK_STREAM)
c_2.connect((ip_add, 25002))
print(2)
qsize = 10
boxQue = queue.Queue(qsize)
img_sent = queue.Queue(qsize)
lock = threading.Lock()
# time.sleep(10)
fps = 0
fps_dis = 0
def recv_box():
lth = np.zeros(shape=(1, ), dtype=np.int64)
while 1:
if boxQue.full():
# print('box is full')
time.sleep(0.1)
else:
recv_into(lth, c_2)
if lth[0] == 0:
lock.acquire()
boxQue.put([0])
lock.release()
continue
arr = np.zeros(shape=(1, lth[0], 7), dtype=np.float32)
recv_into(arr, c_2)
box = arr.tolist()
for i in range(lth[0]):
box[0][i][-1]=np.int64(box[0][i][-1])
lock.acquire()
boxQue.put(box)
lock.release()
# sum_flag = np.zeros(shape=(1,), dtype=np.int32)
# def recv_flag():
# global sum_flag
# recv_into(sum_flag, c)
# print('done')
img_single = queue.Queue(10)
img_cor = queue.Queue(10)
lock_2 = threading.Lock()
def cam_input():
cap = cv2.VideoCapture(0)
flag = cap.isOpened()
print(flag)
while 1:
_, img = cap.read()
cnt = 0
if img_single.full():
cnt += 1
else:
lock_2.acquire()
img_single.put(img)
lock_2.release()
if img_cor.full():
cnt += 1
else:
lock_2.acquire()
img_cor.put(img)
lock_2.release()
if cnt == 2:
time.sleep(0.1)
cap.release()
def cam_send_cor():
# _thread.start_new_thread(recv_flag, ())
cnt_arr = np.zeros(shape=(1,), dtype=np.int32)
# while 1:
# _, img = cap.read()
# send_from(img, c)
# send_from(np.array([np.sum(img)]), c)
# recv_into(cnt_arr, c)
# if cnt_arr[0]>=5:
# break
while 1:
while img_sent.full() or img_cor.empty():
# print('sent is full')
time.sleep(0.1)
lock_2.acquire()
img = img_cor.get()
lock_2.release()
# print(img)
send_from(img, c)
lock.acquire()
img_sent.put(img)
lock.release()
def fps_update_cor():
global fps, fps_dis
while 1:
time.sleep(10)
print("tow fps:", fps)
fps_dis = fps / 10
fps = 0
url = 'http://192.168.1.103:8001/agent/system/refreshDNNQos?qos='+str(fps_dis)
payload = {'message': "1"}
r = requests.post(url, data=payload)
fps_single = 0
fps_dis_single = 0
def fps_single_display():
global fps_single,fps_dis_single
while 1:
time.sleep(10)
fps_dis_single = fps_single / 10
print("single fps:", fps_single)
fps_single = 0
url = 'http://192.168.1.103:8001/agent/system/refreshNodeQos?qos='+str(fps_dis_single)
payload = {'message': "1"}
r = requests.post(url, data=payload)
def cam_single():
global fps_single, fps_dis_single
s = socket(AF_INET, SOCK_STREAM)
s.bind(('', 26000))
s.listen(1)
c, a = s.accept()
print(3)
while (1):
# get a frame
while img_single.empty():
time.sleep(0.1)
lock_2.acquire()
img = img_single.get()
lock_2.release()
# print(111)
send_from(img, c)
# print(222)
lth = np.zeros(1, dtype=np.int64)
recv_into(lth, c)
if not lth[0] == 0:
boxes = np.zeros((1, lth[0], 7), dtype=np.float32)
recv_into(boxes, c)
boxes = boxes.tolist()
for i in range(lth[0]):
boxes[0][i][-1] = np.int64(boxes[0][i][-1])
img = plot_boxes_cv2(img, boxes[0], 'predictions.jpg', class_names)
fps_single += 1
img = cv2.putText(img, 'FPS: {}'.format(fps_dis_single), (100, 100), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 0), 2)
cv2.imshow('ONE:', img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
_thread.start_new_thread(cam_input, ())
_thread.start_new_thread(fps_single_display, ())
_thread.start_new_thread(cam_single, ())
_thread.start_new_thread(recv_box, ())
_thread.start_new_thread(cam_send_cor, ())
_thread.start_new_thread(fps_update_cor, ())
# def get_box():
# while boxQue.empty():
# time.sleep(0.1)
# lock.acquire()
# box = boxQue.get()
# lock.release()
# return box
namesfile = 'data/coco.names'
class_names = load_class_names(namesfile)
while (1):
# get a frame
while img_sent.empty() or boxQue.empty():
# print('sent or box are empty')
time.sleep(0.1)
lock.acquire()
img = img_sent.get()
boxes = boxQue.get()
lock.release()
# print(np.sum(img))
# start = time.time()
if boxes[0] == 0:
pass
else:
img = plot_boxes_cv2(img, boxes[0], 'predictions.jpg', class_names)
img = cv2.putText(img, 'FPS: {}'.format(fps_dis), (100, 100), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 0), 2)
# end = time.time()
# print('time: ', end - start)
cv2.imshow('TWO', img)
fps += 1
# send_from(frame, c_3)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
c.close()
s.close()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment