[英]I got an error when using mkdir command with “ ! ” - Python
使用 mkdir 命令時出現 SyntaxError 消息。 我已經導入 os 庫。 我想運行.mkdir。 我不知道,因為我已經實現了正確的路徑格式。 請幫助我。
import cv2
import time
import uuid
import os
IMAGES_PATH = 'Tensorflow/workspace/images/collectedimages'
lables = ['hello', 'thanks', 'yes', 'no', 'iloveyou']
number_imgs = 15
for lable in lables:
!mkdir {'Tensorflow\workspace\images\collectedimages\\'+lable}
cap = cv2.VideoCapture(0)
print('Collecting imagess for {}'.format(lables))
time.sleep(5)
for imgnum in range(number_imgs):
ret, frame = cap.read()
imgname = os.path.join(IMAGES_PATH, lable, lable+'.'+'{}.jpg'.format(str(uuid.uuid1())))
cv2.imwrite(imgname, frame)
cv2.imshow('frame', frame)
time.sleep()
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
使用os
模塊創建目錄,而不是使用 shell 命令。
import os
directory = "<your_directory_name>"
parent_dir = "T:/<parent_directory>/"
path = os.path.join(parent_dir, directory)
os.mkdir(path)
print("Directory '% s' created" % directory)
如果您在 windows 上運行,則可以使用以下代碼:
import cv2
import time
import uuid
import os
IMAGES_PATH = 'Tensorflow\workspace\images\collectedimages'
lables = ['hello', 'thanks', 'yes', 'no', 'iloveyou']
number_imgs = 15
for lable in labels:
os.mkdir('Tensorflow\workspace\images\collectedimages\\'+lable)
cap = cv2.VideoCapture(0)
print('Collecting imagess for {}'.format(lables))
time.sleep(5)
for imgnum in range(number_imgs):
ret, frame = cap.read()
imgname = os.path.join(IMAGES_PATH, label,
lable+'.'+'{}.jpg'.format(str(uuid.uuid1())))
cv2.imwrite(imgname, frame)
cv2.imshow('frame', frame)
time.sleep()
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
如果您使用的是 ubuntu:
將!mkdir {'Tensorflow\workspace\images\collectedimages\\'+lable}
更改為os.system('sudo mkdir Tensorflow/workspace/images/collectedimages//'+label)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.