Kameraansteuerung

Python auf Einplatinencomputer wie Raspberry Pi, Banana Pi / Python für Micro-Controller
Nicky200
User
Beiträge: 14
Registriert: Mittwoch 19. Februar 2020, 20:00

@einfachTobi: Fehler behoben.
Allerdings wieder eine Fehlermledung:

Traceback (most recent call last):
File "ProjektOpenCV.py", line 76, in <module>
D = dist.euclidean ((tblX, tlblY), (trbrX, trbrY))
NameError: name 'tblX' is not defined

Code:

Code: Alles auswählen

from scipy.spatial import distance as dist
from imutils import perspective
from imutils import contours
import picamera
import RPi.GPIO as GPIO
import time  
import imutils
import cv2
import numpy as np
import argparse 


GPIO.setmode(GPIO.BCM)


GPIO.setup(26, GPIO.IN)
GPIO.input(26) 
GPIO.setup(20, GPIO.OUT) 
GPIO.setup(16, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(7, GPIO.OUT)



   
while True: 
    if GPIO.input(26) == 0: 
        #LOW - Pegel
        
        # Setup the camera such that it closes
        #when we are done with it.  

        print("About take a picture")
        with picamera.PiCamera() as camera:
            camera.resolution = (1280,720)
            camera.capture("/var/www/html/Projekt/Laiserbild.jpg")
            print("Picture taken.") 
    else: 
    	#HIGH - Pegel
        
        def midpoint(ptA, ptB):
            return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5)    
        gray = cv2.imread ("/var/www/html/Projekt/Laiserbild.jpg")
       
        edged = cv2.Canny(gray, 50,100)
        edged = cv2.dilate(edged, None, iterations=1)
        edged = cv2.erode(edged, None, iterations=1)
        
        cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, 
            cv2.CHAIN_APPROX_SIMPLE)
        cnts = imutils.grab_contours (cnts)
        
        (cnts, _) = contours.sort_contours(cnts)
        colors = ((0, 0, 255), (240, 0, 159), (0, 165, 255), (255, 255, 0), (255, 0, 255))
        refObj = None 
        
        for c in cnts:
        
            if cv2.contourArea(c) > 1:
                continue
        
        box = cv2. minAreaRect (c)
        box = cv2. boxPoints (box) if imutils.is_cv2() else  cv2.boxPoints (box)
        box = np.array(box, dtype="int")
        
        box = perspective.order_points(box)
        
        cX = np.average(box[:,0])
        cY = np.average(box[:, 1])
        
        if refObj is None: 
            (tl, tr, br, bl) = box
            (tlblX, tlblY) = midpoint(tl, bl)
            (trbrX, trbrY) = midpoint (tr, br)
            
            D = dist.euclidean ((tblX, tlblY), (trbrX, trbrY))
            refObj = (box, (cX, CY), D / 0.21)
            continue
        
        orig = image.copy()
        cv2.drawContrours(orig, [box.astype("int")],-1, (0,255, 0), 2)
        cv2.drawContours(orig, [refObj[0].astype("int")], -1, (0, 255, 0), 2)
        
        refCoords = np.vstack([refObj[0], refObj[1]])
        objCoords = np.vstack([box, (cX, cY)])
        
        for ((xA, yA), (xB, yB), color) in zip(refCoords, objCoords, colors):
        
            cv2.circle(orig, (int(xA), int(yA)), 5, color, -1)
            cv2.circle(orig, (int(xB), int(yB)), 5, color, -1)
            cv2.line(orig, (int(xA), int(yA)), (int(xB), int(yB)),
                color, 2)
            
            D = dist.euclidean((xA, yA), (xB, yB)) / refObj[2]
            (mX, mY) = midpoint((xA, yA), (xB, yB))
            cv2.putText(orig, "{:.1f}in".format(D), (int(mX), int(mY - 10)),
			cv2.FONT_HERSHEY_SIMPLEX, 0.55, color, 2)
            
            if D <= 0.3:
               GPIO.output(20, GPIO.HIGH)
               GPIO.output(16, GPIO.LOW)
               GPIO.output(12, GPIO.LOW)
               GPIO.output(7, GPIO.LOW)
               
               
            elif (D > 0.3) & (D <= 0.6): 
               GPIO.output(20, GPIO.LOW)
               GPIO.output(16, GPIO.HIGH)
               GPIO.output(12, GPIO.LOW)
               GPIO.output(7, GPIO.LOW)
               
            elif (D > 0.6) & (D <= 0.9): 
               GPIO.output(20, GPIO.LOW)
               GPIO.output(16, GPIO.LOW)
               GPIO.output(12, GPIO.HIGH)
               GPIO.output(7, GPIO.LOW)
               
            elif (D > 0.9 )& (D <= 1.2): 
               GPIO.output(20, GPIO.LOW)
               GPIO.output(16, GPIO.LOW)
               GPIO.output(12, GPIO.HIGH)
               GPIO.output(7, GPIO.LOW)
               
            elif D > 1.2:  
               GPIO.output(20, GPIO.LOW)
               GPIO.output(16, GPIO.LOW)
               GPIO.output(12, GPIO.LOW)
               GPIO.output(7, GPIO.HIGH)
            
        cv2.imshow("image", gray)
        cv2.imshow("Copy", orig)
    time.sleep(0.0001)
GPIO.cleanup()
einfachTobi
User
Beiträge: 512
Registriert: Mittwoch 13. November 2019, 08:38

Wenn dir dieser Fehler nichts sagt, mangelt es entscheidend an den Grundlagen. Mir scheint, dass du keine Ahnung hast, was du da überhaupt machst. Ich persönlich habe wenig Lust dir beim Zusammenschustern und -kopieren von Code zu helfen, den du eh nicht verstehst.
Bitte zunächst die Grundlagen lernen, dann weißt du auch was dieser Fehler bedeutet und wie man ihn behebt: https://docs.python.org/3/tutorial/.
Als weiterer Hinweis: https://www.dropbox.com/s/cqsxfws52gulkyx/drawing.pdf
Nicky200
User
Beiträge: 14
Registriert: Mittwoch 19. Februar 2020, 20:00

Doch, der Fehler sagt mir schon etwas. Habe ihn auch mittlerweile behoben.
Antworten