首页 > 解决方案 > 在服务器上执行 python 脚本

问题描述

我想在服务器上运行这个 python 脚本:

#!C:\Users\Username\Anaconda3\python.exe
import cgi

print("Content-type: text/html\n")
print("""<!DOCTYPE HTML>
    <html>
    <head>
        <meta charset="utf-8">
        <title>Title</title>
    </head>
    <body>""")

import cv2
import os

cam = cv2.VideoCapture(0)
cam.set(3, 640)  # set video width
cam.set(4, 480)  # set video height

face_detector = cv2.CascadeClassifier('haarcascade_frontalface_alt_tree.xml')

# For each person, enter one numeric face id
face_id = 1

print("\n [INFO] Initializing face capture. Look the camera and wait ...")
# Initialize individual sampling face count
count = 0

while (True):

    ret, img = cam.read()
    # img = cv2.flip(img, -1)  # flip video image vertically
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_detector.detectMultiScale(gray, 1.3, 5)

for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
    count += 1

    # Save the captured image into the datasets folder
    cv2.imwrite("dataset/User." + str(face_id) + '.' + str(count) + ".jpg", gray[y:y + h, x:x + w])

    cv2.imshow('image', img)

k = cv2.waitKey(100) & 0xff  # Press 'ESC' for exiting video
if k == 27:
    break
elif count >= 30:  # Take 30 face sample and stop video
    break

# Do a bit of cleanup
print("\n [INFO] Exiting Program and cleanup stuff")
cam.release()
cv2.destroyAllWindows()
print("""</body>
</html>""")

页面呈现,但未cv2导入库。导入库后,脚本结束。你怎么能解决这个问题?OpenCV 通过 Anaconda 安装。

图片

标签: pythonopencv

解决方案


这是因为服务器上没有安装 cv2,如果您可以访问服务器的终端,请尝试以下操作:

pip install opencv-python

推荐阅读