分类 opencv 的文章

openwrt 操作IIC

img_3075-removebg.png
img_3082-removebg.png

固件内置了i2ctools软件包

  1. 用i2cdetect -l 命令来列出所有I2C总线
    root@Widora:/# i2cdetect -l
    i2c-0 i2c 10000900.i2c I2C adapt
  2. 用i2cdetect -F 0 来查看I2C 0号总线启用的功能

    root@Widora:/# i2cdetect -F 0
    Functionalities implemented by /dev/i2c-0:
    I2C yes
    SMBus Quick Command yes
    SMBus Send Byte yes
    SMBus Receive Byte yes
    SMBus Write Byte yes
    SMBus Read Byte yes
    SMBus Write Word yes
    SMBus Read Word yes
    SMBus Process Call yes
    SMBus Block Write yes
    SMBus Block Read no
    SMBus Block Process Call no
    SMBus PEC yes
    I2C Block Write yes
    I2C Block Read yes

  3. 查看I2C 0号总线上挂载的设备

    root@Widora:/# i2cdetect -y 0
    0 1 2 3 4 5 6 7 8 9 a b c d e f
    00: -- -- -- -- -- -- -- -- -- -- -- -- --
    10: -- -- -- -- -- -- -- -- -- -- UU -- -- -- -- --
    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30: -- -- -- -- UU -- -- -- -- -- -- -- -- -- -- --
    40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- --
    50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70: -- -- -- -- -- -- -- --

其中标识为UU的设备(地址分别为0x1a和0x34)表示内核已经加载了相应的驱动,而地址为0x48的I2C设备尚未被内核所驱动。

  1. 在sysfs文件中可查看已被内核驱动的I2C设备的名称

     root@Widora:/# ls /sys/bus/i2c/devices
     0-001a  0-0034  i2c-0
     root@Widora:/# cat /sys/bus/i2c/devices/0-001a/name
     wm8960
     root@Widora:/# cat /sys/bus/i2c/devices/0-0034/name
     codec_wm8960
    
  2. 用i2cdump -y 0 0x48 W 命令来读取地址为0x48的I2C设备(LM75温度传感器)所有寄存器中的内容
 root@Widora:/# i2cdump -y 0 0x48 W
    0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f    0123456789abcdef
    00: 1e 00 4b 00 ff ff ff ff 1e 00 4b 00 ff ff ff ff    ?.K.....?.K.....
    10: 1e 00 4b 00 ff ff ff ff 1e 00 4b 00 ff ff ff ff    ?.K.....?.K.....
    20: 1e 00 4b 00 ff ff ff ff 1e 00 4b 00 ff ff ff ff    ?.K.....?.K.....
    30: 1e 00 4b 00 ff ff ff ff 1e 00 4b 00 ff ff ff ff    ?.K.....?.K.....
    40: 1e 00 4b 00 ff ff ff ff 1e 00 4b 00 ff ff ff ff    ?.K.....?.K.....
    50: 1e 00 4b 00 ff ff ff ff 1e 00 4b 00 ff ff ff ff    ?.K.....?.K.....
    60: 1e 00 4b 00 ff ff ff ff 1e 00 4b 00 ff ff ff ff    ?.K.....?.K.....
  1. 用i2cget -y 0 0x48 0 w 命令来读取地址为0x48的I2C设备0号寄存器中的内容

利用树莓派结合datamark 实现远程移动监控

请输入图片描述

#encoding=utf-8
import cv2
import time
from poster.encode import multipart_encode
import urllib2
import os,shutil
import sys
from urllib2 import Request, urlopen, URLError, HTTPError
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import datetime
import socket
import time

DATAMARK=1
TLINK=2
GIZWITS=3
print "open video0"
cap=cv2.VideoCapture(0)
print cap
pre_frame = None
def post(file):
    register_openers()
    f=open(file, "rb")
    datagen, headers = multipart_encode({"myFile": f})
    request = urllib2.Request("http://trtos.com/web/datamark/upload.php", datagen, headers)
    try:
        response = urllib2.urlopen(request)
        print response.read()
         
    except URLError,e:
        print e.reason
        print e.code




def send(context,name,id):
   if name==DATAMARK:
       context=context+"\n"
       ss=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
       ss.connect(('trtos.com',80))
       msg="POST /php/api.php?id="+id+" HTTP/1.1\n"+ \
            "Content-Type: application/x-www-form-urlencoded\n"+ \
            "Host: trtos.com\n"+ \
            "Content-Length: "+str(len(context))+"\n"+ \
            "Expect: 100-continue\n"+ \
            "Connection: Keep-Alive\n\n\n"+context
       ss.sendall(msg)
       ss.close()
       print msg
   elif name==TLINK:
       ss=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
       ss.connect(('tcp.tlink.io',8647))
       time.sleep(1)
       ss.sendall(id)
       time.sleep(1)
       ss.sendall(context)
       ss.close()
       print id,context,name
   elif name==GIZWITS:
       print "机智云"
      
while(1):
    ret,frame=cap.read()
    curframe=frame.copy()
    k=cv2.waitKey(1)
    gray_img=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    gray_img=cv2.resize(gray_img, (500, 500))
    gray_img=cv2.GaussianBlur(gray_img, (21, 21), 0)
    if pre_frame is None:
        pre_frame = gray_img
    else:
        img_delta = cv2.absdiff(pre_frame, gray_img)
        thresh = cv2.threshold(img_delta, 25, 255, cv2.THRESH_BINARY)[1]
        thresh = cv2.dilate(thresh, None, iterations=2)
        thresh,contours, hierarchy = cv2.findContours(thresh.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
        for c in contours:
            if cv2.contourArea(c) < 3000:
                 print cv2.contourArea(c)
                 continue
            else:
                 (x,y,w,h) = cv2.boundingRect(c)
                 cv2.rectangle(curframe,(x,y),(x+w,y+h),(0,255,0),2)
                 cv2.imwrite("5uT5zGTG.jpg",curframe)
                 post("5uT5zGTG.jpg")
                 shutil.move("5uT5zGTG.jpg","/home/pi/record/"+datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S')+".jpg")
                 send("area:"+str(cv2.contourArea(c)),DATAMARK,"5uT5zGTG")
    pre_frame = gray_img
    time.sleep(1)
cap.release()`

python-opencv环境搭建

使用ubuntu18.2 系统或者其他系统
默认会安装python2.7
sudo apt install python
sudo apt install python-opencv
sudo apt install python-pip2019-11-23 18-12-21 的屏幕截图.png