• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest May 25th, 2019 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #Bibliotheken einbinden
2. import RPi.GPIO as GPIO
3. import time
4. from picamera.array import PiRGBArray
5. from picamera import PiCamera
6. import cv2
7. import numpy as np
8. from PIL import Image
9.
10. while True:
11.     #GPIO Modus (BOARD / BCM)
12.     GPIO.setmode(GPIO.BCM)
13.
14.     #GPIO Pins zuweisen
15.     trigger = 18   #BCM 18
16.     echo = 24      #BCM 24
17.
18.     #Richtung der GPIO-Pins festlegen (IN/OUT)
19.     GPIO.setup(trigger, GPIO.OUT)
20.     GPIO.setup(echo, GPIO.IN)
21.
22.     #define function
23.     #Funktion um die Distanz zu messen
24.     def distance():
25.         # setz Trigger auf HIGH
26.         GPIO.output(trigger, True)
27.
28.         #setzt Trigger nach 0.01ms auf LOW
29.         time.sleep(0.00001)
30.         GPIO.output(trigger, False)
31.
32.         startTime = time.time()
33.         stopTime = time.time()
34.
35.         #speichere startTime
36.         while GPIO.input(echo) == 0:
37.             startTime = time.time()
38.
39.         #speichere Ankunftszeit
40.         while GPIO.input(echo) == 1:
41.             stopTime = time.time()
42.
43.         #Zeit Differenz zwischen Start und Ankunft
44.         TimeElapsed = stopTime - startTime
45.         #mit der Schallgeschwindigkeit (34300 cm/s) multiplizieren
46.         #und durch 2 teilen, da hin und zurueck
47.         distance = (TimeElapsed * 34300) / 2
48.         return distance
49.
50.     #Funktion um ein Bild aufzunehmen
51.     def capture():
52.         camera = PiCamera()
53.         rawCapture = PiRGBArray(camera)
54.
55.         time.sleep(0.1) #gibt der Kamera Zeit zum starten
56.
57.         camera.capture(rawCapture, format="bgr")
58.         return rawCapture.array
59.
60.         width = 736/2
61.         height = 480/2
62.
63.         cv2.rectangle(image,(int(width-width/2),int(height-height/2)),(int(width+width/2),int(height+height/2)),(0,0,0),-1)
64.
65.     abstand = distance()
66.     abstand = round(abstand, 0)
67.     print ('Entfernung = ' +str(abstand) +'cm', abstand)
68.     default_distance = 35
69.
70.     if default_distance != abstand:
71.         ledPin = 17     #BCM 17
72.         uvPin = 27      #BCM 27
73.
74.         #GPIO.setwarnings(False)
75.         GPIO.setmode(GPIO.BCM)
76.         #Richtung der GPIO-Pins festlegen (IN/OUT)
77.         GPIO.setup(ledPin, GPIO.OUT)
78.         GPIO.setup(uvPin, GPIO.OUT)
79.
80.         GPIO.output(ledPin, GPIO.HIGH)
81.         print("LED on")
82.         time.sleep(1)
83.
84.         #Bildaufnahme Bild
85.         image = capture()
86.         cv2.imwrite('/home/sandra/image.jpg',image)
87.         time.sleep(3)
88.
89.         GPIO.output(ledPin, GPIO.LOW)
90.         time.sleep(1)
91.
92.
93.         GPIO.output(uvPin, GPIO.HIGH)
94.         print("UV-LED on")
95.         time.sleep(1)
96.
97.         #Bildaufnahme UV-Bild
98.         uv = capture()
99.         cv2.imwrite('/home/sandra/uv.jpg',uv)
100.         time.sleep(3)
101.
102.         GPIO.output(uvPin, GPIO.LOW)
103.
104.         print("Pictures captured")
105.         GPIO.cleanup()
106.
107.         break
108.
109.     else:
110.         print('nothing')
111.         time.sleep(1)
112.
113.
114. #Bildauswertung
115.
116. #Bild wird leicht verunschärft um eindeutigere Umrisse zu bekommen
117. blur = cv2.bilateralFilter(image,9,75,75)
118. cv2.imwrite('/home/sandra/blur.jpg',blur)
119.
120. #das Bild wird in Grautöne umgewandelt
121. gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)
122. cv2.imwrite('/home/sandra/gray.jpg',gray)
123.
124. #das in Grautöne unterteilte Bild wir zu einem schwarz-weiß Bild umgewandelt
125. thresh = 70
126. bw = cv2.threshold(gray, thresh, 255, cv2.THRESH_BINARY)[1]
127. cv2.imwrite('bw.jpg', bw)
128.
129. #invertiert die Farben eines Bildes
130. #invert = cv2.bitwise_not(bw)
131. #cv2.imwrite('invert.jpg', invert)
132.
133.
134. #UV
135.
136. #UV-Bild wird leicht verunschärft um eindeutigere Umrisse zu bekommen
137. uvblur = cv2.bilateralFilter(uv,9,75,75)
138. cv2.imwrite('/home/sandra/uvblur.jpg',uvblur)
139.
140. #das UV-Bild wird in Grautöne umgewandelt
141. uvgray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)
142. cv2.imwrite('/home/sandra/uvgray.jpg',uvgray)
143.
144. #das in Grautöne unterteilte UV-Bild wir zu einem schwarz-weiß Bild umgewandelt
145. uvbw = cv2.threshold(uvgray, thresh, 255, cv2.THRESH_BINARY)[1]
146. cv2.imwrite('uvbw.jpg', uvbw)
147.
148. #Maske mit Umrisse wird über das UV-Bild gelegt
149. uvnew = np.zeros(image.shape, np.uint8)
150. uvlayer = cv2.bitwise_or(uvimage, uvnew, mask=uvbw)
151. cv2.imwrite('/home/sandra/uvlayer.jpg',uvlayer)
152.
153. #ändert im UV-Bild alles schwarze zu rot
155. uvred[np.where((colour == [0,0,0]).all(axis = 2))] = [0,33,166] #BGR
156. cv2.imwrite('uvred.jpg', uvred)
157.
158.
159. #Maske mit Umrisse wird über das bearbeitete UV-Bild gelegt
160. new = np.zeros(image.shape, np.uint8)
161. final = cv2.bitwise_or(uvred, new, mask=bw)
162. cv2.imwrite('/home/sandra/final.jpg',final)
163.
164. print("edited")
165.
166. whole = 0
167. black = 0
168. red = 0
169. hand = 0
170. clean = 0
171.
172. for pixel in final.getdata():
173.     whole += 1
174.     if pixel == (0, 0, 0): #schwarz # if your image is RGB (if RGBA, (0, 0, 0, 255) or so
175.         black += 1
176.     else:
177.         if pixel == (166, 33, 0): #rot
178.             red += 1
179.
180. hand = whole-black
181. clean = hand-red
182.
183.
184. print('[in pixel] hand: ' +str(hand) +'clean:' +str(clean) +', dirty: ' +str(red))
185.
186. clean = (clean/hand)*100
187. red = (red/hand)*100
188.
189. print('[in Prozent] clean: ' +str(clean) +'% , dirty: ' +str(red) +'%')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top