Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 New to Python, help with saving image from camera
#1
I'm new to Python, but I decided to write a program that takes a picture of a person's face and saves it as "who.jpg". When I run this code, I'm able to see the camera and rectangles around the face, but the "who.jpg" is blank, and it's 0 kb in size. What can I do to have the "who.jpg" take picture of the face and save it? I just need to take the picture of the face one time.

import cv2
import sys

cascPath = 'haarcascade_frontalface_default.xml'
faceCascade = cv2.CascadeClassifier(cascPath)

video_capture = cv2.VideoCapture('rtsp://ituser:ituser@172.16.2.35/mpeg4/media.amp')

while True:
    # Capture frame-by-frame
    ret, frame = video_capture.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30)
    )

    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

    # Display the resulting frame
    img = cv2.imshow('Server Room', frame)
    cv2.imwrite("who.jpg", img)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()

Thanks,

Tony

I guess I can't use imread for videos, and this says I need to use ffmpeg. Am I correct?

https://stackoverflow.com/questions/3726...ng-a-video
Quote
#2
The cv2.imwrite("who.jpg", img) is inside the loop, so every img shown on the screen
is written into that filename. I don´t think that is what you want and i assume the code can´t write that fast.
In addition cv2.imwrite() returns an integer, if i´m not wrong 0 means ok, 1 means error.

Untested and only an idea:
    # Display the resulting frame
    img = cv2.imshow('Server Room', frame)
 
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    if cv2.waitKey(1) & 0xFF == ord('p'):
        err = cv2.imwrite("who.jpg", img)
        print(error)
        break
Should save an image if you press p key and then quit.
Quote
#3
I got it figured it out, and its working now! Thanks everybody for the help.

""" Importing libraries """

import cv2
import datetime
import os

""" Declaring variables """

cascPath = 'haarcascade_frontalface_default.xml'
faceCascade = cv2.CascadeClassifier(cascPath)
camera = cv2.VideoCapture('rtsp://ituser:ituser@172.16.2.35/mpeg4/media.amp')
path = './Images'

""" Do this while the camera is working """

while (camera.isOpened()):
    ret, frame = camera.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.2,
        minNeighbors=5,
        minSize=(30, 30)
    )

    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 255), 2)
        date = datetime.datetime.now()
        date = (date.strftime("%m-%d-%Y_%H%M%S"))
        jpeg = date + '.jpg'
        cv2.imwrite(os.path.join(path,jpeg), frame)

    cv2.imshow('SCA Server Room', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

""" Finally, release the capture and close windows """

camera.release()
cv2.destroyAllWindows()

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  pi camera stream is upside down delta1071 3 204 Sep-11-2019, 11:35 AM
Last Post: metulburr
  Camera animation to Text file (CINEMA 4D) vbz 0 154 Aug-13-2019, 07:39 AM
Last Post: vbz
  Saving python cmd output into a text file with colours kapilan15 2 424 Jan-25-2019, 06:25 PM
Last Post: metulburr
  How to extract temperature value of pixels of a thermal image recorded by IR camera hamacvan 1 1,191 Jan-13-2019, 06:06 PM
Last Post: Larz60+
  Controling a Canon dslr camera on Windows Leon 14 2,680 Dec-31-2018, 10:41 AM
Last Post: Leon
  Python 3.6.5 - Saving files Mradr 1 712 Jul-22-2018, 12:21 AM
Last Post: Larz60+
  Getting values from Arduino into python and saving it as csv file Yamin 1 2,770 Mar-20-2018, 05:45 AM
Last Post: woooee
  Camera Image Data annakin4 0 801 Jan-24-2018, 05:50 PM
Last Post: annakin4
  Camera & Audio Output Windy_i 2 803 Dec-20-2017, 07:20 PM
Last Post: nilamo
  Programming using 4 camera trabis03 0 1,072 Jun-19-2017, 02:58 AM
Last Post: trabis03

Forum Jump:


Users browsing this thread: 1 Guest(s)