• Python 摄像头 树莓派 USB mjpb


    import cv2
    import urllib.request
    import numpy as np
    import sys
    host = "192.168.1.109:8080" #在这里记得修改IP,否则是无法调用的,刚刚浏览器输入的地址
    if len(sys.argv)>1:
        host = sys.argv[1]
    hoststr = 'http://' + host + '/?action=stream'
    print('Streaming ' + hoststr)
    
    print('Print Esc to quit')
    stream=urllib.request.urlopen(hoststr)
    bytes=bytes()
    while True:
        bytes+=stream.read(1024)
        a = bytes.find(b'xffxd8')
        b = bytes.find(b'xffxd9')
        if a!=-1 and b!=-1:
            jpg = bytes[a:b+2]
            bytes= bytes[b+2:]
            #flags = 1 for color image
            i = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),flags=1)
           # print i.shape
            cv2.imshow("wjw",i)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                exit(0)
    import cv2
    import urllib.request
    import numpy as np
    import sys
    import base64
    import time
    import os
    
    host = "192.168.1.109:8080" #在这里记得修改IP,否则是无法调用的,刚刚浏览器输入的地址
    if len(sys.argv)>1:
        host = sys.argv[1]
    hoststr = 'http://' + host + '/?action=stream'
    print('Streaming ' + hoststr)
    
    print('Print Esc to quit')
    stream=urllib.request.urlopen(hoststr)
    bytes=bytes()
    while True:
        bytes+=stream.read(1024)
        a = bytes.find(b'xffxd8')
        b = bytes.find(b'xffxd9')
        if a!=-1 and b!=-1:
            jpg = bytes[a:b+2]
            bytes= bytes[b+2:]
            #flags = 1 for color image
            i = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),flags=1)
           # print i.shape
            cv2.imshow("wjw",i)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                exit(0)
    
    
            name = str(time.time())
            p = '/home/wjw/Desktop/123/'+name+'.jpg'
            # 判断目录是否存在
            if not os.path.exists(os.path.split(p)[0]):
                # 目录不存在创建,makedirs可以创建多级目录
                os.makedirs(os.path.split(p)[0])
            try:
                # 保存数据到文件
                with open(p, 'wb') as f:
                    f.write(jpg)
                print('保存成功')
            except Exception as e:
                print('保存失败', e)
  • 相关阅读:
    计算机硬件发展史
    17.Java8新特性_传统时间格式化的线程安全问题
    13. Java8新特性_Stream API 练习
    12. Java8新特性_Stream_归约与收集
    11.Java8新特性_Stream_查找与匹配
    10.Java8新特性_Stream_排序
    9. Java8新特性_Stream_映射
    8. Java8新特性_Stream_筛选与切片
    CentOS 安装 Python3
    CentOS7安装图形桌面系统(GNOME / KDE / Cinnamon / MATE / Xfce)
  • 原文地址:https://www.cnblogs.com/wjw1014/p/11145936.html
Copyright © 2020-2023  润新知