3 Star 1 Fork 0

WZY99/迷宫机器人

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
camera.py 2.16 KB
一键复制 编辑 原始数据 按行查看 历史
WZY99 提交于 2020-10-07 14:50 . v1.7.1
#!/usr/bin/env python
import cv2
import base64
from server import Server, get_host_ip
# global image for muilt-process
g_cap = None
g_img = None
HOST = get_host_ip()
class Camera:
def __init__(self, width, height, device=0):
self.width = width
self.height = height
self.device = device
self.cap = None
self.init()
def init(self):
global g_cap
if g_cap is None:
self.cap = cv2.VideoCapture(self.device)
self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, self.height)
self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, self.width)
else:
self.cap = g_cap
def read(self):
try:
return g_img
except Exception as e:
print(e)
def spin(self):
global g_img
g_img = self.cap.read()
def set_height(self, height):
self.height = height
self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
def set_width(self, width):
self.width = width
self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
def __del__(self):
self.cap.release()
class FPV:
def __init__(self, width, height, device=0):
self.camera = Camera(width, height, device)
self.server = Server(HOST, port=5555)
def to_base64(self, img):
_, buf = cv2.imencode(".jpg", img)
return base64.b64encode(buf)
def run(self, client):
while True:
img = g_img
img_code = self.to_base64(img)
try:
recv = cli.recv(2)
print('recv', recv)
if recv == b'BG': # BEGIN
print('send', len(img_code))
cli.send(struct.pack("l", len(img_code)))
recv = cli.recv(2)
print('recv', recv)
if recv == b'OK':
cli.send(img_code)
else:
continue
elif recv == b'SP': # STOP
flag = 0
else:
continue
except Exception as e:
print(e)
cli.close()
return
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wzy-99/maze-robot.git
git@gitee.com:wzy-99/maze-robot.git
wzy-99
maze-robot
迷宫机器人
master

搜索帮助