Pārlūkot izejas kodu

improve detection

Torsten Simon 2 gadi atpakaļ
vecāks
revīzija
1c170c2428
3 mainītis faili ar 51 papildinājumiem un 14 dzēšanām
  1. 1 1
      docker-compose.yml
  2. 5 1
      install.sh
  3. 45 12
      src/data-writer.py

+ 1 - 1
docker-compose.yml

@@ -16,7 +16,7 @@ services:
       ports:
         - 80:80
       volumes:
-        - images-data:/usr/share/nginx/images
+        - ./src/images:/usr/share/nginx/html
   grafana:
       image: grafana/grafana:7.5.7
       ports:

+ 5 - 1
install.sh

@@ -15,4 +15,8 @@ sudo journalctl -fu solar
 
 # UNSORTED
 # git clone https://github.com/ljk53/pytorch-rpi && cd pytorch-rpi
-# LIBTORCH_VARIANT=armv7l-cxx11-abi-shared-without-deps ./build_libtorch.sh
+# LIBTORCH_VARIANT=armv7l-cxx11-abi-shared-without-deps ./build_libtorch.sh
+
+sudo apt-get install -y libimlib2-dev
+git clone https://github.com/auerswal/ssocr
+cd ssocr && sudo make install

+ 45 - 12
src/data-writer.py

@@ -22,9 +22,10 @@ class SolarMonitor:
             self.initCamera()
         
     def parse(self, img): 
-        custom_oem=r'--oem 3 --psm 11'
+        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', output_type=Output.DICT)
         print(data)
         results = []
         for i in range(len(data['text'])):
@@ -75,19 +76,49 @@ class SolarMonitor:
         return cv2.threshold(image, 140, 255, cv2.THRESH_BINARY)[1]
         return image
     def thresholding2(self, image):
+        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
+        # Taking a matrix of size 5 as the kernel
+        kernel = np.ones((3, 3), np.uint8)
         
-        image = cv2.medianBlur(image,7)
+        # The first parameter is the original image,
+        # kernel is the matrix with which image is
+        # convolved and third parameter is the number
+        # of iterations, which will determine how much
+        # you want to erode/dilate a given image.
+        #image = cv2.dilate(image, kernel, iterations=1)
+        #image = cv2.erode(image, kernel, iterations=1)
+        image = cv2.convertScaleAbs(image, alpha=2.5, beta=-210)
+        image = (255 - image)
+        image = cv2.medianBlur(image,3)
         #image = np.invert(image)
         return image
     
     def crop(self, image):
-        top=314
-        left=230
-        height= 150
-        width=400
-        return image[top : (top + height) , left: (left + width)]
+        #top=384
+        #left=210
+        #height= 60
+        #width=230
+        #return image[top : (top + height) , left: (left + width)]
+        pt_A = [228, 392]
+        pt_B = [228, 430]
+        pt_C = [419, 424]
+        pt_D = [420, 386]
+        width_AD = np.sqrt(((pt_A[0] - pt_D[0]) ** 2) + ((pt_A[1] - pt_D[1]) ** 2))
+        width_BC = np.sqrt(((pt_B[0] - pt_C[0]) ** 2) + ((pt_B[1] - pt_C[1]) ** 2))
+        maxWidth = max(int(width_AD), int(width_BC))
 
 
+        height_AB = np.sqrt(((pt_A[0] - pt_B[0]) ** 2) + ((pt_A[1] - pt_B[1]) ** 2))
+        height_CD = np.sqrt(((pt_C[0] - pt_D[0]) ** 2) + ((pt_C[1] - pt_D[1]) ** 2))
+        maxHeight = max(int(height_AB), int(height_CD))
+        input_pts = np.float32([pt_A, pt_B, pt_C, pt_D])
+        output_pts = np.float32([[0, 0],
+                        [0, maxHeight - 1],
+                        [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
 
     def initCamera(self):
         from picamera import PiCamera
@@ -96,12 +127,13 @@ class SolarMonitor:
         )
         self.camera.awb_mode = 'off'
         self.camera.awb_gains = (1.5, 2.0)
-        self.camera.shutter_speed = 60000
+        self.camera.shutter_speed = 50000
         self.camera.iso = 800
+        self.camera.rotation = 180
 
     def capture(self): 
         file = "images/" + str(datetime.now()) + ".jpg"
-        self.camera.capture(file, format="jpeg", quality=25)
+        self.camera.capture(file, format="jpeg", quality=10)
         print("Captured.")
         img = cv2.imread(file)
         img = self.crop(img)
@@ -147,9 +179,10 @@ class SolarMonitor:
             print(results)
             if results:
                 self.writeData(results)
-            cv2.imwrite(file + '_r.jpg', img, [cv2.IMWRITE_JPEG_QUALITY, 25])
-            # os.unlink(file)
-            time.sleep(2)    
+                img = cv2.putText(img, str(results), (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,255,255), 2, cv2.LINE_AA)
+            cv2.imwrite(file + '_r.jpg', img, [cv2.IMWRITE_JPEG_QUALITY, 85])
+            #os.unlink(file)
+            time.sleep(5)
 
 solar = SolarMonitor(test = False)
 while True: