2017-09-27 1 views
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 파일에 인쇄 된 값을 쓰는 것입니다.

답변

1

CSV 형식으로 데이터를 작성하는 것은 매우 일반적인 작업입니다. library called csv을 사용할 수 있습니다.

은이 샘플 코드를 개선하기 위해 제안을 할 의사가 없습니다

output.writerow((testingPath, x, y, w, h, brandname, len(boxes))) 
+0

이 줄을 마지막 두 줄

dat = str([x, y, w, h, brandname, len(boxes)]) output.write("{},{}\n".format(testingPath, "".join(dat))) 

를 교체하여 출력 변수를 CSV 작가

output = csv.writer(open(args["index"], "w")) 

확인 코드 품질 및/또는 가독성. –

+0

고마워 ....이 변경 작업. – Apricot