0
올바르게 실행되고 값을 인쇄하는 opencv python 프로그램이 있습니다. 그러나 인쇄 된 값을 CSV 파일에 쓰려고하면 오류가 발생합니다.여러 배열을 csv로 작성하는 파이썬
for testingPath in paths.list_images(args["testing"]):
# load the image and make predictions
image = cv2.imread(testingPath)
boxes = detector(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
# loop over the bounding boxes and draw them
for b in boxes:
(x, y, w, h) = (b.left(), b.top(), b.right(), b.bottom())
cv2.rectangle(image, (x, y), (w, h), (0, 255, 0), 2)
#print(basename(testingPath),"CX:"+str(x),"CY:"+str(y),"Width:"+str(w),"Height:"+str(h),brandname,"Number of brands detected: {}".format(len(boxes))) -----this prints all the required values without problem on the console
내가이 일을 시도 : 다음은 코드는 다음과 같이
ap.add_argument("-i", "--index", required=True, help="Path to directory of output")
output = open(args["index"], "w")
루프를 사용 : 나는 루프 시작의 전에 인수를 추가
for testingPath in paths.list_images(args["testing"]):
# load the image and make predictions
image = cv2.imread(testingPath)
#filename = testingPath[testingPath.rfind("/") + 1:]
boxes = detector(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
#print(basename(testingPath), brandname,"Number of brands detected: {}".format(len(boxes)))
# loop over the bounding boxes and draw them
for b in boxes:
(x, y, w, h) = (b.left(), b.top(), b.right(), b.bottom())
cv2.rectangle(image, (x, y), (w, h), (0, 255, 0), 2)
#print(basename(testingPath),"CX:"+str(x),"CY:"+str(y),"Width:"+str(w),"Height:"+str(h),brandname,"Number of brands detected: {}".format(len(boxes)))
dat = str([x, y, w, h, brandname, len(boxes)])
output.write("{},{}\n".format(testingPath, "".join(dat)))
위 코드는 다음과 같은 값을 출력합니다 :
/home/mycomp/VideoExtract/28157.jpg,[83, 349, 164, 383, 'Pirelli', 1]
[] 대괄호를 제거하려고합니다. 원하는 작업은 csv/text 파일에 인쇄 된 값을 쓰는 것입니다.
이 줄을 마지막 두 줄
를 교체하여 출력 변수를 CSV 작가
확인 코드 품질 및/또는 가독성. –
고마워 ....이 변경 작업. – Apricot