简体   繁体   English

执行python代码时没有任何反应(Eigenface)

[英]nothing happens when executing python code(Eigenface)

I'm new in Image-processing field and using Opencv library.Blow is a code for face recognition using Eigenface Algorithm in Python(data set included in the blow link). 我是图像处理领域的新手,并且使用Opencv库.Blow是使用Python中的Eigenface算法(打击链接中包含的数据集)进行人脸识别的代码。 I have problem with getting result of executing Eigenface code from learnopencv , here is all i got when i run Eigenface.py 我对从learningopencv获取执行Eigenface代码的结果有问题,这是我运行Eigenface.py时得到的全部

Reading images from F:\\learnopencv-master\\EigenFace\\images... 从F:\\ learnopencv-master \\ EigenFace \\ images中读取图像...

I'm using : 我正在使用 :

winpython-3.4.2.2.amd64 winpython-3.4.2.2.amd64

Any idea will be helpful...! 任何想法都会有所帮助...! Thank you 谢谢

from __future__ import print_function
import os
import sys
import cv2
import numpy as np

# Create data matrix from a list of images
def createDataMatrix(images):
    print("Creating data matrix",end=" ... ")
    ''' 
    Allocate space for all images in one data matrix.
  The size of the data matrix is

  ( w  * h  * 3, numImages )

  where,

  w = width of an image in the dataset.
  h = height of an image in the dataset.
  3 is for the 3 color channels.
  '''

    numImages = len(images)
    sz = images[0].shape
    data = np.zeros((numImages, sz[0] * sz[1] * sz[2]), dtype=np.float32)
    for i in xrange(0, numImages):
        image = images[i].flatten()
        data[i,:] = image

    print("DONE")
    return data

# Read images from the directory
def readImages(path):
  print("Reading images from " + path, end="...")
  # Create array of array of images.
  images = []
  # List all files in the directory and read points from text files one by one
  for filePath in sorted(os.listdir(path)):
    fileExt = os.path.splitext(filePath)[1]
    if fileExt in [".jpg", ".jpeg"]:

      # Add to array of images
      imagePath = os.path.join(path, filePath)
      im = cv2.imread(imagePath)

      if im is None :
        print("image:{} not read properly".format(imagePath))
      else :
          # Convert image to floating point
          im = np.float32(im)/255.0
          # Add image to list
          images.append(im)
          # Flip image 
          imFlip = cv2.flip(im, 1);
          # Append flipped image
          images.append(imFlip)

  numImages = len(images) / 2
  # Exit if no image found
  if numImages == 0 : print("No images found") 
  sys.exit(0)

  print(str(numImages) + " files read.")
  return images

# Add the weighted eigen faces to the mean face 
def createNewFace(*args):
    # Start with the mean image
    output = averageFace

    # Add the eigen faces with the weights
    for i in xrange(0, NUM_EIGEN_FACES):
        '''
        OpenCV does not allow slider values to be negative. 
        So we use weight = sliderValue - MAX_SLIDER_VALUE / 2
        ''' 
        sliderValues[i] = cv2.getTrackbarPos("Weight" + str(i), "Trackbars");
        weight = sliderValues[i] - MAX_SLIDER_VALUE/2
        output = np.add(output, eigenFaces[i] * weight)

    # Display Result at 2x size
    output = cv2.resize(output, (0,0), fx=2, fy=2)
    cv2.imshow("Result", output)

def resetSliderValues(*args):
    for i in xrange(0, NUM_EIGEN_FACES):
        cv2.setTrackbarPos("Weight" + str(i), "Trackbars", MAX_SLIDER_VALUE/2); 
    createNewFace()

if __name__ == '__main__':

    # Number of EigenFaces
    NUM_EIGEN_FACES = 10

    # Maximum weight
    MAX_SLIDER_VALUE = 255

    # Directory containing images
    dirName = "F:\learnopencv-master\EigenFace\images"

    # Read images
    images = readImages(dirName)

    # Size of images
    sz = images[0].shape

    # Create data matrix for PCA.
    data = createDataMatrix(images)

    # Compute the eigenvectors from the stack of images created
    print("Calculating PCA ", end="...")
    mean, eigenVectors = cv2.PCACompute(data, mean=None, maxComponents=NUM_EIGEN_FACES)
    print ("DONE")

    averageFace = mean.reshape(sz)

    eigenFaces = []; 

    for eigenVector in eigenVectors:
        eigenFace = eigenVector.reshape(sz)
        eigenFaces.append(eigenFace)

    # Create window for displaying Mean Face
    cv2.namedWindow("Result", cv2.WINDOW_AUTOSIZE)

    # Display result at 2x size
    output = cv2.resize(averageFace, (0,0), fx=2, fy=2)
    cv2.imshow("Result", output)

    # Create Window for trackbars
    cv2.namedWindow("Trackbars", cv2.WINDOW_AUTOSIZE)

    sliderValues = []

    # Create Trackbars
    for i in xrange(0, NUM_EIGEN_FACES):
        sliderValues.append(MAX_SLIDER_VALUE/2)
        cv2.createTrackbar( "Weight" + str(i), "Trackbars", MAX_SLIDER_VALUE/2, MAX_SLIDER_VALUE, createNewFace)

    # You can reset the sliders by clicking on the mean image.
    cv2.setMouseCallback("Result", resetSliderValues);

    print('''Usage:
    Change the weights using the sliders
    Click on the result window to reset sliders
    Hit ESC to terminate program.''')

    cv2.waitKey(0)
    cv2.destroyAllWindows()

Your example exits silently because you have sys.exit(0) in your function readImages, which executes unconditionally. 您的示例无提示退出,因为函数readImages中有sys.exit(0) ,该函数无条件执行。

So instead of: 所以代替:

  if numImages == 0 : print("No images found") 
  sys.exit(0)

It should be: 它应该是:

  if numImages == 0 :
      print("No images found")
      sys.exit(0)

This is correct on GitHub . 在GitHub上正确的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在/ usr / local / bin /中执行python shebang脚本时没有任何反应 - Nothing happens when executing a python shebang script in /usr/local/bin/ 当我运行这个 python / pytube 代码时没有任何反应 - Nothing happens when I run this python / pytube code Python - 如果 1 分钟内没有任何反应,继续执行代码 - Python - If nothing happens for 1 minute, proceed code 执行脚本时什么也没发生? - While executing script nothing happens? Python-尝试运行代码时,什么也没有发生。 我什至没有出错 - Python - When trying to run the code nothing happens. I don't even get an error 在python中,如何在执行正常(退出代码0)但没有结果(没有打印内容)时修复代码? - In python how to fix the code when it is executing fine (exit code 0) but with no results (nothing printing)? 使用Python在OpenCV中使用EigenFace - EigenFace in opencv using python OpenCV和本征面识别器,Python - opencv and eigenface recognizer, python macos 中 python 中带有 open cv 的代码:进程在终端中启动,但没有任何反应 - Code with open cv in python in macos: process is launched in terminal but nothing happens QT代码,运行程序时什么也没发生 - QT code, nothing happens when I run the program
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM