Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Bibliotheken einbinden
- import RPi.GPIO as GPIO
- import time
- from picamera.array import PiRGBArray
- from picamera import PiCamera
- import cv2
- import numpy as np
- from PIL import Image
- while True:
- #GPIO Modus (BOARD / BCM)
- GPIO.setmode(GPIO.BCM)
- #GPIO Pins zuweisen
- trigger = 18 #BCM 18
- echo = 24 #BCM 24
- #Richtung der GPIO-Pins festlegen (IN/OUT)
- GPIO.setup(trigger, GPIO.OUT)
- GPIO.setup(echo, GPIO.IN)
- #define function
- #Funktion um die Distanz zu messen
- def distance():
- # setz Trigger auf HIGH
- GPIO.output(trigger, True)
- #setzt Trigger nach 0.01ms auf LOW
- time.sleep(0.00001)
- GPIO.output(trigger, False)
- startTime = time.time()
- stopTime = time.time()
- #speichere startTime
- while GPIO.input(echo) == 0:
- startTime = time.time()
- #speichere Ankunftszeit
- while GPIO.input(echo) == 1:
- stopTime = time.time()
- #Zeit Differenz zwischen Start und Ankunft
- TimeElapsed = stopTime - startTime
- #mit der Schallgeschwindigkeit (34300 cm/s) multiplizieren
- #und durch 2 teilen, da hin und zurueck
- distance = (TimeElapsed * 34300) / 2
- return distance
- #Funktion um ein Bild aufzunehmen
- def capture():
- camera = PiCamera()
- rawCapture = PiRGBArray(camera)
- time.sleep(0.1) #gibt der Kamera Zeit zum starten
- camera.capture(rawCapture, format="bgr")
- return rawCapture.array
- width = 736/2
- height = 480/2
- cv2.rectangle(image,(int(width-width/2),int(height-height/2)),(int(width+width/2),int(height+height/2)),(0,0,0),-1)
- abstand = distance()
- abstand = round(abstand, 0)
- print ('Entfernung = ' +str(abstand) +'cm', abstand)
- default_distance = 35
- if default_distance != abstand:
- ledPin = 17 #BCM 17
- uvPin = 27 #BCM 27
- #GPIO.setwarnings(False)
- GPIO.setmode(GPIO.BCM)
- #Richtung der GPIO-Pins festlegen (IN/OUT)
- GPIO.setup(ledPin, GPIO.OUT)
- GPIO.setup(uvPin, GPIO.OUT)
- GPIO.output(ledPin, GPIO.HIGH)
- print("LED on")
- time.sleep(1)
- #Bildaufnahme Bild
- image = capture()
- cv2.imwrite('/home/sandra/image.jpg',image)
- time.sleep(3)
- GPIO.output(ledPin, GPIO.LOW)
- time.sleep(1)
- GPIO.output(uvPin, GPIO.HIGH)
- print("UV-LED on")
- time.sleep(1)
- #Bildaufnahme UV-Bild
- uv = capture()
- cv2.imwrite('/home/sandra/uv.jpg',uv)
- time.sleep(3)
- GPIO.output(uvPin, GPIO.LOW)
- print("Pictures captured")
- GPIO.cleanup()
- break
- else:
- print('nothing')
- time.sleep(1)
- #Bildauswertung
- #Bild wird leicht verunschärft um eindeutigere Umrisse zu bekommen
- blur = cv2.bilateralFilter(image,9,75,75)
- cv2.imwrite('/home/sandra/blur.jpg',blur)
- #das Bild wird in Grautöne umgewandelt
- gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)
- cv2.imwrite('/home/sandra/gray.jpg',gray)
- #das in Grautöne unterteilte Bild wir zu einem schwarz-weiß Bild umgewandelt
- thresh = 70
- bw = cv2.threshold(gray, thresh, 255, cv2.THRESH_BINARY)[1]
- cv2.imwrite('bw.jpg', bw)
- #invertiert die Farben eines Bildes
- #invert = cv2.bitwise_not(bw)
- #cv2.imwrite('invert.jpg', invert)
- #UV
- #UV-Bild wird leicht verunschärft um eindeutigere Umrisse zu bekommen
- uvblur = cv2.bilateralFilter(uv,9,75,75)
- cv2.imwrite('/home/sandra/uvblur.jpg',uvblur)
- #das UV-Bild wird in Grautöne umgewandelt
- uvgray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)
- cv2.imwrite('/home/sandra/uvgray.jpg',uvgray)
- #das in Grautöne unterteilte UV-Bild wir zu einem schwarz-weiß Bild umgewandelt
- uvbw = cv2.threshold(uvgray, thresh, 255, cv2.THRESH_BINARY)[1]
- cv2.imwrite('uvbw.jpg', uvbw)
- #Maske mit Umrisse wird über das UV-Bild gelegt
- uvnew = np.zeros(image.shape, np.uint8)
- uvlayer = cv2.bitwise_or(uvimage, uvnew, mask=uvbw)
- cv2.imwrite('/home/sandra/uvlayer.jpg',uvlayer)
- #ändert im UV-Bild alles schwarze zu rot
- uvred = cv2.imread('uvlayer.jpg')
- uvred[np.where((colour == [0,0,0]).all(axis = 2))] = [0,33,166] #BGR
- cv2.imwrite('uvred.jpg', uvred)
- #Maske mit Umrisse wird über das bearbeitete UV-Bild gelegt
- new = np.zeros(image.shape, np.uint8)
- final = cv2.bitwise_or(uvred, new, mask=bw)
- cv2.imwrite('/home/sandra/final.jpg',final)
- print("edited")
- whole = 0
- black = 0
- red = 0
- hand = 0
- clean = 0
- for pixel in final.getdata():
- whole += 1
- if pixel == (0, 0, 0): #schwarz # if your image is RGB (if RGBA, (0, 0, 0, 255) or so
- black += 1
- else:
- if pixel == (166, 33, 0): #rot
- red += 1
- hand = whole-black
- clean = hand-red
- print('[in pixel] hand: ' +str(hand) +'clean:' +str(clean) +', dirty: ' +str(red))
- clean = (clean/hand)*100
- red = (red/hand)*100
- print('[in Prozent] clean: ' +str(clean) +'% , dirty: ' +str(red) +'%')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement