Torsten Simon 2 vuotta sitten
vanhempi
commit
803a06228b
1 muutettua tiedostoa jossa 48 lisäystä ja 36 poistoa
  1. 48 36
      src/data-writer.py

+ 48 - 36
src/data-writer.py

@@ -22,23 +22,26 @@ class SolarMonitor:
         if not test:
             self.initCamera()
         
-    def parse(self, img): 
-        custom_oem=r'--oem 3 --psm 7'
-        # https://github.com/adrianlazaro8/Tesseract_sevenSegmentsLetsGoDigital
-        data = pytesseract.image_to_data(img, lang='lets', config=custom_oem, output_type=Output.DICT)
-        #data = pytesseract.image_to_data(img, lang='letsgodigital', config=custom_oem, output_type=Output.DICT)
-        #data = pytesseract.image_to_data(img, config=custom_oem, output_type=Output.DICT)
-        print(data)
-        results = []
-        for i in range(len(data['text'])):
-            text = data['text'][i].strip('.,-_')
-            text = re.sub('[^0-9]', '', text)
-            if text:
-                results.append(text)
+    def parse(self, images):
+        results = [] 
+        for img in images:
+            custom_oem=r'--oem 3 --psm 7'
+            # https://github.com/adrianlazaro8/Tesseract_sevenSegmentsLetsGoDigital
+            data = pytesseract.image_to_data(img, lang='lets', config=custom_oem, output_type=Output.DICT)
+            #data = pytesseract.image_to_data(img, lang='letsgodigital', config=custom_oem, output_type=Output.DICT)
+            #data = pytesseract.image_to_data(img, config=custom_oem, output_type=Output.DICT)
+            print(data)
+            for i in range(len(data['text'])):
+                text = data['text'][i].strip('.,-_')
+                text = re.sub('[^0-9]', '', text)
+                if text:
+                    results.append(text)
+                    break
+        
         if len(results) == 2:
-            results = list(map(lambda x: int(x) / 10.,results ))
-            if results[0] < 80 and results[1] < 900:
-                return results
+                results = list(map(lambda x: int(x) / 10.,results ))
+                if results[0] < 80 and results[1] < 900:
+                    return results[0:2]
         return None
     def getMasked(self, image):        
         pixel_values = image.reshape((-1, 1))
@@ -88,7 +91,9 @@ class SolarMonitor:
         #image = (255 - image)
         # image = self.getMasked(image)
         # image = cv2.dilate(image, kernel, iterations=1)
-        image = cv2.medianBlur(image,3)
+        # image = cv2.medianBlur(image,5)
+        kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
+        # image = cv2.filter2D(image, -1, kernel)
         #thresh = cv2.adaptiveThreshold(image,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,51,1)
         # detect the contours on the binary image using cv2.CHAIN_APPROX_NONE
         #contours, hierarchy = cv2.findContours(image=thresh, mode=cv2.RETR_TREE, method=cv2.CHAIN_APPROX_NONE)
@@ -99,22 +104,10 @@ class SolarMonitor:
         # cv2.drawContours(image, contours, -1, (0,255,0), 3)
         #image = np.invert(image)
         return image
-    
-    def crop(self, image):
-        #top=384
-        #left=210
-        #height= 60
-        #width=230
-        #return image[top : (top + height) , left: (left + width)]
-        pt_TL = [260, 190]
-        pt_BL = [260, 242]
-        pt_BR = [500, 240]
-        pt_TR = [500, 190]
+    def cropByBounds(self, image, pt_TL, pt_BL, pt_BR, pt_TR): 
         width_AD = np.sqrt(((pt_TL[0] - pt_TR[0]) ** 2) + ((pt_TL[1] - pt_TR[1]) ** 2))
         width_BC = np.sqrt(((pt_BL[0] - pt_BR[0]) ** 2) + ((pt_BL[1] - pt_BR[1]) ** 2))
         maxWidth = max(int(width_AD), int(width_BC))
-
-
         height_AB = np.sqrt(((pt_TL[0] - pt_BL[0]) ** 2) + ((pt_TL[1] - pt_BL[1]) ** 2))
         height_CD = np.sqrt(((pt_BR[0] - pt_TR[0]) ** 2) + ((pt_BR[1] - pt_TR[1]) ** 2))
         maxHeight = max(int(height_AB), int(height_CD))
@@ -124,8 +117,26 @@ class SolarMonitor:
                         [maxWidth - 1, maxHeight - 1],
                         [maxWidth - 1, 0]])
         M = cv2.getPerspectiveTransform(input_pts,output_pts)
-        image = cv2.warpPerspective(image,M,(maxWidth, maxHeight),flags=cv2.INTER_LINEAR)
-        return image
+        image_crop = cv2.warpPerspective(image,M,(maxWidth, maxHeight),flags=cv2.INTER_LINEAR)
+        return image_crop
+    def crop(self, image):
+        #top=384
+        #left=210
+        #height= 60
+        #width=230
+        #return image[top : (top + height) , left: (left + width)]
+        pt_TL = [280, 190]
+        pt_BL = [280, 242]
+        pt_BR = [380, 240]
+        pt_TR = [380, 190]
+        left = self.cropByBounds(image, pt_TL, pt_BL, pt_BR, pt_TR)
+        pt_TL = [380, 190]
+        pt_BL = [380, 242]
+        pt_BR = [480, 240]
+        pt_TR = [480, 190]
+        right = self.cropByBounds(image, pt_TL, pt_BL, pt_BR, pt_TR)
+        
+        return [left, right]
 
     def initCamera(self):
         from picamera import PiCamera
@@ -183,14 +194,15 @@ class SolarMonitor:
                 cv2.imwrite(file + '_r.jpg', img)
         else:
             [file, img] = solar.capture()
-            img = self.thresholding2(img)
+            # img = self.thresholding2(img)
             results = self.parse(img)
             print(results)
+            print(img[1].shape)
             if results:
                 self.writeData(results)
-                img = cv2.putText(img, str(results), (75, 22), cv2.FONT_HERSHEY_SIMPLEX, 0.25, (255, 255, 255, 255, 255), 1, cv2.LINE_AA)
-            cv2.imwrite(file + '_r.jpg', img, [cv2.IMWRITE_JPEG_QUALITY, 35])
-            time.sleep(5)
+                img_result = cv2.putText(img[1], str(results), (15, 7), cv2.FONT_HERSHEY_SIMPLEX, 0.25, (255, 255, 255), 1, cv2.LINE_AA)
+            cv2.imwrite(file + '_r.jpg', img_result, [cv2.IMWRITE_JPEG_QUALITY, 35])
+            time.sleep(3)
 
 solar = SolarMonitor(test = False)
 while True: