Sunday, February 11, 2018

Vision Video


Getting started with video:
http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html#display-video


The following example will load up a video from disk . It uses the VideoCapture object.
  • Its argument can be either the device index or the name of a video file. Device usually is the index is just the number to specify which camera to use.  This line below sets the video source to use an existing file.


#------------------------------------------------------------
import numpy as np
import cv2 as cv
cap = cv.VideoCapture('output.mp4')
while(cap.isOpened()):
       ret, frame = cap.read()
       gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
       cv.imshow('frame',gray)
       if cv.waitKey(1) & 0xFF == ord('q'):
             break
cap.release()
cv.destroyAllWindows()

#------------------------------------------------------------

[link]  - tweak for VideoCapture

VideoWriter([filename, fourcc, fps, frameSize, isColor)
  • filename – Name of the output video file.
  • fourcc – 4-character code of codec used to compress the frames. 
  • fps – Framerate of the created video stream.
  • frameSize – Size of the video frames.
  • isColor – If it is not zero, the encoder will expect and encode color frames, otherwise it will work with grayscale frames (the flag is currently supported on Windows only).


The read() function

  • reads one frame from the video source, which in this example is the webcam. 

This returns two things:
  • The actual video frame read (one frame on each loop) 
  • A return code (True/False). If frame is read correctly.  It tells us if we have run out of frames, which will happen if we are reading from a file. This doesn’t matter when reading from the webcam, since we can record forever, so we will ignore it.
Reading from web camera:

#------------------------------------------------------------
while(True):  
     # Capture frame-by-frame  
     ret, frame = cap.read()  
    # frame come here  
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)  
 
    # Display the resulting frame  
    cv.imshow('frame',gray)  
    if cv.waitKey(1) & 0xFF == ord('q'):      
          break

# When everything done, release the capture
cap.release()
cv.destroyAllWindows()

#------------------------------------------------------------


Saving images from video camera input to a file:

#------------------------------------------------------------

import cv2
import numpy as np
#This line sets the video source to the default webcam, which OpenCV
cap = cv2.VideoCapture(0)
w = cap.get(cv2.CAP_PROP_FRAME_WIDTH);
h = cap.get(cv2.CAP_PROP_FRAME_HEIGHT);
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('output.mp4',fourcc, 15.0, (int(w),int(h)))

while True:
        ret,frame = cap.read()
        #---------for gray video--------
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        out.write(frame)

        #Display the resulting frame
        cv2.imshow('myvideo',frame)
        cv2.imshow('myvideo',gray)
        cv2.waitKey(0)      
        k = cv2.waitKey(0) & 0xFF
        if k == ord('q'):    # wait for 'q' key to exit break
                break
cap.release()
out.release()
cv2.destroyAllWindows()


#------------------------------------------------------------

Saturday, February 10, 2018

Vision Overview

In my earlier Python post I mentioned how easy is to install Open CV

OpenCV is the most popular library for computer vision. Originally written in C/C++, it now provides bindings for Python. OpenCV uses machine learning algorithms to search for faces within a picture. For something as complicated as a face, there isn’t one simple test that will tell you if it found a face or not. Instead, there are thousands of small patterns/features that must be matched. The algorithms break the task of identifying the face into thousands of smaller, bite-sized tasks, each of which is easy to solve
Here is what you can do with OpenCV

Well before we start mentioning the provided examples on web site, it is good to have the API search tool - i.e. reading and writing images API


Here as part of the gui features we will read an image from disk. Likewise, we can write a copy of the image to the disk.

Here are some of the methods we will use:

imread
  • file name
  • flag on way image should be read
imshow
  • named window name
  • image
imwrite
  • file name on disk
  • image



#------------------------------------------------------------
import cv2
import numpy as np

#practice numpy
#https://www.w3resource.com/python-exercises/numpy/index.php#EDITOR

# IMREAD_GRAYSCALE = 0
# IMREAD_COLOR = 1
# IMREAD_UNCHANGED = -1

 img = cv2.imread('img.JPG',cv2.IMREAD_GRAYSCALE)

# ----------showing with opencv.........

cv2.imshow('my image',img)
k = cv2.waitKey(0) & 0xFF

if k == 27: # wait for ESC key to exit
      cv2.destroyAllWindows()
elif k == ord('s'): # wait for 's' key to save and exit
       cv2.imwrite('img2.JPG',img) # writing the image
       cv2.destroyAllWindows()

#------------------------------------------------------------





  •  mouse as paint brush
  •             https://docs.opencv.org/master/db/d5b/tutorial_py_mouse_handling.html


    Wednesday, February 7, 2018

    python everywhere

    Python seems like the hot thing. I have been learning and experimenting.

    I use The following package manager for macOS:  https://brew.sh/

     /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 

    use example: - brew install python

    anaconda https://www.youtube.com/watch?v=8JiWEZEnJ40

    Install anaconda:

    • https://repo.continuum.io/archive/Anaconda3-4.2.0-Windows-x86_64.exe 
    • https://repo.continuum.io/archive/Anaconda3-4.2.0-MacOSX-x86_64.pkg


    condo update anaconda

    to install opencv:

    conda install --channel https://conda.anaconda.org/menpo opencv3

    confirm the install from python command line
            import cv2
            cv2.__version__

    spyder is an IDE for perl - for macOs from the cmd line enter to start it:
                spyder&

    console : print "hello"
    editor: run by select f9
    inspector : debugging

    see spyder perl tutorial https://www.youtube.com/watch?v=J5GevIHNctM


    http://www.numpy.org/
    • https://www.w3resource.com/python-exercises/numpy/index.php

      https://www.scipy.org


    active state
    python tutorial https://pythonprogramming.net/introduction-to-python-programming/

    https://www.youtube.com/watch?list=PLQVvvaa0QuDe8XSftW-RAxdo6OmaeL85M&time_continue=163&v=IX6mc9l6tY4

    https://www.youtube.com/watch?v=5aP9Bl9hcqI

    https://docs.cs50.net/2016/fall/notes/8/week8.html
    https://www.cs.cornell.edu/courses/cs1110/2017fa/lectures/index.php 
    python class http://stanfordpython.com/#lecture
    http://arc-ts.umich.edu/mini-course-introduction-to-python-sept-11-14/ python https://stanford.edu/~schmit/cme193/ python

    https://github.com/wgong/py4kids