Use python to turn on the camera and color detection method

  • 2020-12-05 17:16:01
  • OfStack

In the last two weeks, I didn't speak because I was busy with my personal project. It was so desolate... Last week, due to the project, I saw that Python was widely used and it was particularly easy to use, so this site also started to learn Python... Now I will report today's learning results

The site operating environment unbuntu 14.0.4

First of all, let's first install Python. I used 2.7, which is actually very simple. One line of instructions is OK

sudo apt-get install python-dev

In general, when installing the system, python has already come with it, so there is no need to do this step. OK, let's go ahead and install python-ES16en. Later, we need to use the library of opencv, and one line of instruction is enough.

sudo apt-get install python-opencv

After the completion of the operation, let's start, first of all, we open the camera to show the face, not to speak more, on the code, vim pythonpractice.py open vim, copy following code can be (courtesy note python is strict indenting, I am four Spaces indent, you do not copy the wrong) :lo


lmport cv2
import numpy as np# Add modules and matrix modules 
cap=cv2.VideoCapture(0)
 Open the camera, if open the local video, same opencv1 Well, let's just replace 0 with 0 (" XXX .avi")
while(1): # get a frame 
 ret, frame = cap.read() # show a frame 
 cv2.imshow("capture", frame) 
 if cv2.waitKey(1) & 0xFF == ord('q'):  
  break
cap.release()
cv2.destroyAllWindows()
# Release and destroy Windows 

Save the exit

python pythonpractice.py

Small face can appear on your screen, add a few lines of interesting code below, to achieve the blue background detection, I have a bottle of blue pulse, just do a little experiment.


import cv2
import numpy as np
cap = cv2.VideoCapture(0)# set blue thresh
lower_blue=np.array([78,43,46])
upper_blue=np.array([110,255,255])
while(1): # get a frame and show 
 ret, frame = cap.read() 
 cv2.imshow('Capture', frame) # change to hsv model 
 hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # get mask 
 mask = cv2.inRange(hsv, lower_blue, upper_blue) 
 cv2.imshow('Mask', mask) # detect blue 
 res = cv2.bitwise_and(frame, frame, mask=mask) 
 cv2.imshow('Result', res) 
 if cv2.waitKey(1) & 0xFF == ord('q'):  
  breakcap.release()
cv2.destroyAllWindows()

Similarly, python ES43en. py run 1, you can change the phone into a blue background to detect the following, the following time to give you to understand, the code is very simple, only a few lines of simple procedures.

Here is the code for a complex dot color recognition


#!/usr/bin/python
# -*- coding: utf-8 -*-
import cv2
import numpy as np
import time
readlower=np.array([156,179,144])
readupper=np.array([180,255,255])
readlower1 = np.array([0, 128, 146])
readupper2 = np.array([5, 255, 255])
lowerarry=[[readlower,readupper,'red'],[readlower1,readupper2,'red1']]
capture=cv2.VideoCapture('4.mp4')
while True:
 ret,frame=capture.read()
 print frame.shape
 frame=cv2.resize(frame,(640,480))
 if ret==False:
  print("video is erro")
 #cv2.imshow('xiaorun',frame)
 hsv=cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
 for colormin,colermax,name in lowerarry:
  mask=cv2.inRange(hsv,colormin,colermax)
  #res = cv2.bitwise_and(frame, frame, mask=mask)
 #mask=cv2.erode(mask,None,iterations=1)
 mask=cv2.dilate(mask,None,iterations=25)
 ret, binary = cv2.threshold(mask,15, 255, cv2.THRESH_BINARY)
 cv2.imshow('result',binary)
 kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (21, 7))
 closed = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel)
 cv2.imshow('closed', closed)
 #erode = cv2.erode(closed, None, iterations=4)
 #cv2.imshow('erode', erode)
 dilate = cv2.dilate(closed, None, iterations=50)
 cv2.imshow('dilate', dilate)
 _,contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
 #res=_.copy()
 for con in contours:
  x, y, w, h = cv2.boundingRect(con) #  The contour is decomposed into the upper-left corner coordinates, width and height of the identified object 
  #  Draw a rectangle on the image (picture, upper-left coordinates, lower-right coordinates, color, line width) 
  cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0,0), 3)

 cv2.imshow('res',frame)
 key=cv2.waitKey(1)
 if key==ord('q'):
  break

This site just want to explain the following, 1 must learn to use, any 1 programming language is two days on the direct start, step-by-step learning grammar, so I do not know when to go out, I wish you play high in machine vision


Related articles: