0

I am trying to detect the closed eye movement in a thermal video to study sleep state. I am able to detect the closed eye region in a face then I am able to save each eye frame later it's converted to give me just the eyelash region but i am unable to save eyelash images with iterative numbering like i did for the eye region.

import numpy as np
import cv2
import scipy
import glob
import os
from pylab import *
from matplotlib import pyplot as plt
from PIL import Image
from collections import Counter
import time

blur_radius = 1.0
threshold = 50


video = cv2.VideoCapture('12.avi')
while True:
        ret, frame = video.read()
        frame = frame[:,1:600]
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        gray = cv2.GaussianBlur(gray, (15,15), 0)
        (minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(gray)
        image = frame
        (x,y) = maxLoc
        cv2.circle(image, maxLoc, 15, (255, 0, 0), 2)
        cv2.rectangle(image,(maxLoc),(390,190),(0,255,0),2)
        roi = frame [y:190,x:390]
        roi = cv2.resize(roi, None, fx=4, fy=4, interpolation=cv2.INTER_AREA)
        count = 0
        while True:
                cv2.imwrite("eye%d.tif" % count,roi)
                count += 1
        for eye in glob.glob("*.tif"):
                im=Image.open(eye)
                gray = im.convert('L')
                bw = gray.point(lambda x: 0 if x<128 else 255, '1')
                count1 = 0
                while True:
                        cv2.imwrite("lashes%d.tif" % count,bw)
                        count += 1
                        count2 = 0
                        for eye in glob.glob("*.tif"):
                                os.remove("eye%d.tif" %count2)
                                count2 += 1                         
        try:
                roi = cv2.resize(roi, None, fx=4, fy=4, interpolation=cv2.INTER_AREA)
                cv2.imshow("Eye",roi)
                cv2.imshow("Eyecorner", image)
        except:
                print''

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

video.release()
cv2.destroyAllWindows()

cropped eye region extracted eyelash

This is the error i am receiving when i run this code

Traceback (most recent call last):
  File "D:\Acads\7.1 Sem\BTP\Data\Thermal videos\after_getting_eye.py", line 39, in <module>
    cv2.imwrite("lashes%d.tif" % count,bw)
TypeError: img is not a numpy array, neither a scalar

once i get these images, i am thinking of using this

diff = ImageChops.difference(im2, im1).getbbox()

to find the difference between previous eyelash image and present eyelash image. if the diff has some value apart from none, then it suggest that there is a eyelash movement which in return proves that there is a eye movement behind the closed lids. Also can anyone tell me a quick way without saving these images and directly work on them as the run time is getting so long and is this a feasible way to detect eye movement when the lid is closed? Any help is kindly appreciated

the_guy
  • 99
  • 3
  • 15

3 Answers3

0

EDIT:

The function cv2.imwrite receives a variable bw which in your code is a PIL image. This format is not compatible with the OpenCV imwrite.

this

Community
  • 1
  • 1
NAmorim
  • 706
  • 6
  • 12
  • The error is neither a image nor a scalar when i tried to print bw it shows `` this. Do you know any way i can save bw iteratively for all the images of eye – the_guy Feb 02 '17 at 05:22
  • However, **PIL** isn't an opencv image format [this](http://stackoverflow.com/questions/14134892/convert-image-from-pil-to-opencv-format) – NAmorim Feb 02 '17 at 10:00
0

Declare the folder in which you would like to store/save all your processed images sequentially.

To save iterative images without over-writing them, you have to perform all your operations within a for loop.

Follow the algorithm as follows:

import glob         

required library. See HERE for more

jpg_files = glob.glob("C:/Users/Desktop/Input_images/*.jpg")
#----Input images are present in this location

a = 'C:/Users/Desktop/Output_folder'
#----Processed output images are saved in this location

#----initializing the for loop
for i in range(len(jpg_files)):
    x=jpg_files[i]
    #---- 'x' contains the input image for every iteration

    #-----
    #-- Here is where you write the code to process your images--
    #-----

    xy = str(jpg_files[(i)])
    #-----'xy' is the new extension you add for every iterated image

    cv2.imwrite('a' + xy, final_image )
    #----- Your procesed image 'final_image ' is stored in location 'a' along with the extension 'xy'.

Hope this helps :)

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
  • Still the same error. I know how to save files using iterative numbering using Imwrite, in the earlier part of code i am saving .jpg files using the same process but i don't know how to do the same using imsave from PIL – the_guy Feb 02 '17 at 05:20
  • did you try it out the same process for `imsave` in PIL? – Jeru Luke Feb 02 '17 at 05:28
  • Yes i did `Traceback (most recent call last): File "D:\Acads\7.1 Sem\BTP\Data\Thermal videos\test_3.py", line 97, in cv2.imwrite('a' + xy, bw) TypeError: img is not a numpy array, neither a scalar` The same error again – the_guy Feb 02 '17 at 06:07
  • HI @JeruLuke , I'm trying to open all the TIFF image file from one location and wanted to place it on the other folder as it is. I have tried your code but its throwing an error like :- **TypeError: img is not a numpy array, neither a scalar** Would be appreciate if you can help me as to why does it error happening!? – Jitesh Vacheta Sep 20 '18 at 14:02
0

I found a method to save image using PIL iteratively. This is working fine for me

for eye in glob.glob("*.tif"):
                file, ext = os.path.splitext(eye)
                im=Image.open(eye)
                gray = im.convert('L')
                bw = gray.point(lambda x: 0 if x<128 else 255, '1')
                bw.save(file + ".tif","JPEG")
the_guy
  • 99
  • 3
  • 15