os.popen('110', '110') 抛出值错误:无效模式"110"



我正在尝试制作一个python脚本,将实时网络摄像头馈馈线转换为ascii类型的图像。到目前为止,代码如下:

import cv2
import os
########################################################################
# SETTINGS #
SHOW_REAL_VIDEO = False   # Set this to True to get real camera video from cv2
########################################################################

def convert_row_to_ascii(row):
# 17-long
ORDER = (' ', '.', "'", ',', ':', ';', 'c', 'l',
'x', 'o', 'k', 'X', 'd', 'O', '0', 'K', 'N')
return tuple(ORDER[int(x / (255 / 16))] for x in row)[::-1]

def convert_to_ascii(input_grays):
return tuple(convert_row_to_ascii(row) for row in input_grays)

def print_array(input_ascii_array):
os.system("clear")
print('n'.join((''.join(row) for row in input_ascii_array)), end='')

cap = cv2.VideoCapture(0)
while(cv2.waitKey(1) & 0xFF != ord('q')):
# Get screensize for reduction
screen_height, screen_width = os.popen('110', '110r').read().split()
# Get image data
ret, frame = cap.read()
# Convert data to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Reduce grayscale array to proper resolution
reduced = cv2.resize(gray, (int(screen_width), int(screen_height)))
# Plug in reduced resolution numpy array for ascii converter func
converted = convert_to_ascii(reduced)
print_array(converted)
# Display the resulting frame
if SHOW_REAL_VIDEO:
cv2.imshow('frame', reduced)
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

它一直给我这样的错误:

Traceback (most recent call last):
File "G:/asciicamera/main.py", line 31, in <module>
screen_height, screen_width = os.popen('110', '110').read().split()
File "C:pythonlibos.py", line 978, in popen
raise ValueError("invalid mode %r" % mode)
ValueError: invalid mode '110'
[ WARN:0@2.007] global D:aopencv-pythonopencv-pythonopencvmodulesvideoiosrccap_msmf.cpp (539) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
谁能告诉我为什么会这样?我试着改变帧的值几次,但它仍然给出这个错误。我是不是漏写了一行代码?

由于你使用的是Windows系统,你可以通过以下方式获得屏幕分辨率:

from win32api import GetSystemMetrics
print("Width =", GetSystemMetrics(0))
print("Height =", GetSystemMetrics(1))

最新更新