zoukankan      html  css  js  c++  java
  • 第7次实践作业

    第7次实践作业

    第19小组 rm-f队

    一、在树莓派中安装opencv库

    opencv我在第6次实验中安装过了,编译源码的方式太慢了,这边用pip安装

    同时纠正下我的第6次实验博客“遇到的问题”中对安装版本的认识,4B可以安装opencv4,在这一次实验遇到的问题中具体讲。

    首先安装依赖

    pip3 install --upgrade setuptools
    pip3 install numpy Matplotlib
    
    sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev
    sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
    sudo apt-get install libxvidcore-dev libx264-dev
    sudo apt-get install libgtk2.0-dev libgtk-3-dev
    sudo apt-get install libatlas-base-dev
    sudo apt install libqt4-test
    

    然后安装opencv
    这样默认安装最新版

    pip3 install opencv-python

    安装成功

    二、使用opencv和python控制树莓派的摄像头

    示例代码

    # import the necessary packages
    from picamera.array import PiRGBArray
    from picamera import PiCamera
    import time
    import cv2
     
    # initialize the camera and grab a reference to the raw camera capture
    camera = PiCamera()
    rawCapture = PiRGBArray(camera)
     
    # allow the camera to warmup
    time.sleep(0.1) 
     
    # grab an image from the camera
    camera.capture(rawCapture, format="bgr")
    image = rawCapture.array
     
    # display the image on screen and wait for a keypress
    cv2.imshow("Image", image)
    cv2.waitKey(0)
    

    代码中感光时间不够长
    time.sleep(0.1) 处仅为0.1秒,如果拍照环境比较暗,例如下图1,效果就不太好,建议将感光时间稍微改长一点,例如图2,同样环境,感光时间是2s

    图1

    图1

    图2

    图2

    下面这个是第6次实验中我已经完成过了的

    通过摄像头实时拍摄查看视频

    import cv2
    
    cap = cv2.VideoCapture(0)
    while(1):
        ret, frame = cap.read()
        cv2.imshow("capture", frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    cap.release()
    cv2.destroyAllWindows() 
    

    三、利用树莓派的摄像头实现人脸识别

    1.facerec_on_raspberry_pi.py

    facerec_on_raspberry_pi.py

    # This is a demo of running face recognition on a Raspberry Pi.
    # This program will print out the names of anyone it recognizes to the console.
    # To run this, you need a Raspberry Pi 2 (or greater) with face_recognition and
    # the picamera[array] module installed.
    # You can follow this installation instructions to get your RPi set up:
    # https://gist.github.com/ageitgey/1ac8dbe8572f3f533df6269dab35df65
    
    import face_recognition
    import picamera
    import numpy as np
    
    # Get a reference to the Raspberry Pi camera.
    # If this fails, make sure you have a camera connected to the RPi and that you
    # enabled your camera in raspi-config and rebooted first.
    camera = picamera.PiCamera()
    camera.resolution = (320, 240)
    output = np.empty((240, 320, 3), dtype=np.uint8)
    
    # Load a sample picture and learn how to recognize it.
    print("Loading known face image(s)")
    obama_image = face_recognition.load_image_file("obama_small.jpg")
    obama_face_encoding = face_recognition.face_encodings(obama_image)[0]
    
    # Initialize some variables
    face_locations = []
    face_encodings = []
    
    while True:
    
        print("Capturing image.")
        # Grab a single frame of video from the RPi camera as a numpy array
        camera.capture(output, format="rgb")
    
        # Find all the faces and face encodings in the current frame of video
        face_locations = face_recognition.face_locations(output)
    
        print("Found {} faces in image.".format(len(face_locations)))
        face_encodings = face_recognition.face_encodings(output, face_locations)
    
        # Loop over each face found in the frame to see if it's someone we know.
        for face_encoding in face_encodings:
    
            # See if the face is a match for the known face(s)
            match = face_recognition.compare_faces([obama_face_encoding], face_encoding)
            name = "<Unknown Person>"
    
            if match[0]:
                name = "Barack Obama"
            print("I see someone named {}!".format(name))
    

    代码所在目录下应放一张用于比对的照片,文件名obama_small.jpg

    2.facerec_from_webcam_faster.py

    facerec_from_webcam_faster.py

    import face_recognition
    import cv2
    import numpy as np
    
    
    # This is a demo of running face recognition on live video from your webcam. It's a little more complicated than the
    # other example, but it includes some basic performance tweaks to make things run a lot faster:
    #   1. Process each video frame at 1/4 resolution (though still display it at full resolution)
    #   2. Only detect faces in every other frame of video.
    
    # PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
    # OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
    # specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.
    
    # Get a reference to webcam #0 (the default one)
    video_capture = cv2.VideoCapture(0)
    
    # Load a sample picture and learn how to recognize it.
    obama_image = face_recognition.load_image_file("obama.jpg")
    obama_face_encoding = face_recognition.face_encodings(obama_image)[0]
    
    # Load a second sample picture and learn how to recognize it.
    biden_image = face_recognition.load_image_file("biden.jpg")
    biden_face_encoding = face_recognition.face_encodings(biden_image)[0]
    
    # Create arrays of known face encodings and their names
    known_face_encodings = [
        obama_face_encoding,
        biden_face_encoding
    ]
    
    known_face_names = [
        "Barack Obama",
        "Joe Biden"
    ]
    
    
    
    # Initialize some variables
    face_locations = []
    face_encodings = []
    face_names = []
    
    process_this_frame = True
    
    while True:
    
        # Grab a single frame of video
        ret, frame = video_capture.read()
    
        # Resize frame of video to 1/4 size for faster face recognition processing
        small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
    
        # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
        rgb_small_frame = small_frame[:, :, ::-1]
    
        # Only process every other frame of video to save time
        if process_this_frame:
    
            # Find all the faces and face encodings in the current frame of video
            face_locations = face_recognition.face_locations(rgb_small_frame)
            face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
    
            face_names = []
            for face_encoding in face_encodings:
                # See if the face is a match for the known face(s)
                matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
                name = "Unknown"
    
                # # If a match was found in known_face_encodings, just use the first one.
                # if True in matches:
                #     first_match_index = matches.index(True)
                #     name = known_face_names[first_match_index]
                # Or instead, use the known face with the smallest distance to the new face
                face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
                best_match_index = np.argmin(face_distances)
                if matches[best_match_index]:
                    name = known_face_names[best_match_index]
    
                face_names.append(name)
    
        process_this_frame = not process_this_frame
    
        # Display the results
        for (top, right, bottom, left), name in zip(face_locations, face_names):
            # Scale back up face locations since the frame we detected in was scaled to 1/4 size
            top *= 4
            right *= 4
            bottom *= 4
            left *= 4
    
            # Draw a box around the face
            cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
    
            # Draw a label with a name below the face
            cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
            font = cv2.FONT_HERSHEY_DUPLEX
            cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
    
        # Display the resulting image
        cv2.imshow('Video', frame)
    
        # Hit 'q' on the keyboard to quit!
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    
    # Release handle to the webcam
    video_capture.release()
    cv2.destroyAllWindows()
    
    

    四、结合微服务的进阶任务

    1.安装Docker

    下载安装脚本

    curl -fsSL https://get.docker.com -o get-docker.sh
    

    执行安装脚本(使用阿里云镜像)

    sh get-docker.sh --mirror Aliyun

    将当前用户加入docker用户组

    sudo usermod -aG docker $USER

    尝试下查看docker版本

    重启过后,docker指令之前就不需要加sudo了

    (2).配置docker的镜像加速

    具体请参考我的第1次作业博客

    sudo nano /etc/docker/daemon.json

    编辑完成后,restart一下docker

    service docker restart
    

    (3).定制自己的opencv镜像

    首先拉取镜像

    docker pull sixsq/opencv-python
    

    运行这个镜像

    docker run -it sixsq/opencv-python /bin/bash
    

    在容器中,pip安装 "picamera[array]" dlib face_recognition

    pip install "picamera[array]" dlib face_recognition
    

    安装成功,退出容器
    然后commit

    编写Dockerfile

    FROM zqzopencv
    MAINTAINER ZhuQingzhang031702426
    RUN mkdir /myapp
    WORKDIR /myapp
    COPY myapp .
    

    build

    docker build -t myopencv .
    

    (4).运行容器执行facerec_on_raspberry_pi.py

    docker run -it --device=/dev/vchiq --device=/dev/video0 --name facerec myopencv
    root@38afdcc52062:/myapp# ls
    biden.jpg  facerec_from_webcam_faster.py  facerec_on_raspberry_pi.py  obama.jpg  obama_small.jpg
    root@38afdcc52062:/myapp# python3 facerec_on_raspberry_pi.py
    

    如果不加--device=/dev/vchiq参数
    则会出现以下报错

    * failed to open vchiq instance
    

    (5).附加选做:opencv的docker容器中运行facerec_from_webcam_faster.py

    在Windows系统中安装Xming
    安装过程一路默认即可

    (https://sourceforge.net/projects/xming/)

    检查树莓派的ssh配置中的X11是否开启

    cat /etc/ssh/sshd_config

    putty中勾起X11选项

    然后使用Putty的ssh登录树莓派
    查看DISPLAY环境变量值
    printenv

    可以看到
    DISPLAY=localhost:10.0

    然后编写run.sh

    #sudo apt-get install x11-xserver-utils
    xhost +
    docker run -it 
            --net=host 
            -v $HOME/.Xauthority:/root/.Xauthority 
            -e DISPLAY=:10.0  
            -e QT_X11_NO_MITSHM=1 
            --device=/dev/vchiq 
            --device=/dev/video0 
            --name facerecgui 
            myopencv 
    	python3 facerec_from_webcam_faster.py
    

    在putty中用ssh运行

    sh run.sh
    

    同样,也可以在vnc中运行

    编写启动脚本

    runvnc.sh

    #sudo apt-get install x11-xserver-utils
    xhost +
    docker run -it 
    	-v /tmp/.X11-unix:/tmp/.X11-unix 
    	-e DISPLAY=$DISPLAY 
    	-e QT_X11_NO_MITSHM=1 
      	--device=/dev/vchiq 
    	--device=/dev/video0 
    	--name facerecguivnc 
    	myopencv 
    	python3 facerec_from_webcam_faster.py
    

    然后运行

    sh runvnc.sh
    

    五、遇到的问题

    1.关于OpenCV的版本

    使用版本4的时候可能出现以下问题

    Traceback (most recent call last):
      File "/home/pi/Desktop/opencv.py", line 1, in <module>
        import cv2
      File "/home/pi/.local/lib/python3.7/site-packages/cv2/__init__.py", line 3, in <module>
        from .cv2 import *
    ImportError: /home/pi/.local/lib/python3.7/site-packages/cv2/cv2.cpython-37m-arm-linux-gnueabihf.so: undefined symbol: __atomic_fetch_add_8
    

    在上一篇博客我的第6次实验博客“遇到的问题”中,我的描述是4B可能和opencv4不太兼容,所以当时我回退了版本到3,解决了。

    这边更正下安装4消除这个问题的做法

    遇到这个问题需要手动加载一个库文件

    sudo nano .bashrc
    添加:export LD_ sudo nano .bashrc PRELOAD=/usr/lib/arm-linux-gnueabihf/libatomic.so.1

    这样就不会报错了。

    六、在线协作

    | 031702426 | 朱庆章 | 负责实际操作 |
    | 031702428 | 潘海东 | 查找资料并提供了问题1的解决方案 |
    | 031702405 | 陈梦雪 | 查找资料 |

    主要通过qq聊天和屏幕分享协作

    屏幕分享

    讨论大作业选题

    总的来说,这次实验难度不大,opencv我在上一次实验中就已经配过了,基本没有遇到难题。

  • 相关阅读:
    MySQL多表查询
    多表关联
    MySQL数据类型 约束
    初识数据库
    socker server和 event
    os 模块 和 os模块下的path模块
    sys 模块
    time 模块
    目录规范

  • 原文地址:https://www.cnblogs.com/Jorgensen/p/13022047.html
Copyright © 2011-2022 走看看