Jelajahi Sumber

docker stuff

Torsten Simon 2 tahun lalu
induk
melakukan
431b167a08
11 mengubah file dengan 177 tambahan dan 295 penghapusan
  1. 0 1
      .gitignore
  2. 11 5
      Dockerfile
  3. 0 99
      data-writer-opencv.py
  4. 0 79
      data-writer.py
  5. 2 2
      docker-compose.yml
  6. 157 0
      src/data-writer-opencv.py
  7. 6 0
      src/entrypoint.sh
  8. 1 1
      src/requirements.txt
  9. 0 71
      src/test-cv.py
  10. 0 27
      src/test.py
  11. 0 10
      src/test2.py

+ 0 - 1
.gitignore

@@ -1,3 +1,2 @@
 *.jpg
-camera/
 camera/

+ 11 - 5
Dockerfile

@@ -1,9 +1,15 @@
 # syntax=docker/dockerfile:1
 
-FROM python:3.8-buster
-RUN apt-get upgrade && apt-get -y install gcc
+FROM debian:bullseye
+RUN apt-get update && apt-get -y install wget git gcc python3-pip tesseract-ocr build-essential cmake pkg-config libjpeg-dev libtiff5-dev libpng-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libfontconfig1-dev libcairo2-dev libgdk-pixbuf2.0-dev libpango1.0-dev libgtk2.0-dev libgtk-3-dev libatlas-base-dev gfortran libhdf5-dev libhdf5-serial-dev libhdf5-103 python3-pyqt5 python3-dev -y
+WORKDIR /tmp
+RUN git clone https://github.com/adrianlazaro8/Tesseract_sevenSegmentsLetsGoDigital && cd Tesseract_sevenSegmentsLetsGoDigital && mv 'Trained data'/* /usr/share/tesseract-ocr/4.00/tessdata/
 WORKDIR /app
-COPY src/ . 
+RUN wget https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh
+RUN chmod +x wait-for-it.sh
+COPY src/requirements.txt . 
+RUN export READTHEDOCS=True
 RUN pip3 install -r requirements.txt
-
-CMD [ "python3", "test-cv.py"]
+COPY src/ .
+RUN chmod +x entrypoint.sh
+CMD [ "./entrypoint.sh"]

+ 0 - 99
data-writer-opencv.py

@@ -1,99 +0,0 @@
-import numpy.core.multiarray
-import cv2
-import pytesseract
-import numpy as np
-import time
-from datetime import datetime
-from picamera import PiCamera
-from pytesseract import Output
-
-import cv2
-import pytesseract
-from pytesseract import Output
-import numpy as np
-import re
-def parse(image): 
-    custom_oem=r'--oem 3 --psm 11'
-    # https://github.com/adrianlazaro8/Tesseract_sevenSegmentsLetsGoDigital
-    data = pytesseract.image_to_data(img, lang='lets', 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)
-    if len(results) == 2:
-        results = list(map(lambda x: int(x) / 10.,results ))
-        if results[0] < 100 and results[1] < 1000:
-            return results
-    return None
-def thresholding(image):
-    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
-    
-    pixel_values = image.reshape((-1, 1))
-    pixel_values = np.float32(pixel_values)
-    print(pixel_values.shape)
-    # define stopping criteria
-    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.2)
-    # number of clusters (K)
-    k = 3
-    _, labels, (centers) = cv2.kmeans(pixel_values, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
-    # convert back to 8 bit values
-    centers = np.uint8(centers)
-
-    # flatten the labels array
-    labels = labels.flatten()
-    # convert all pixels to the color of the centroids
-    segmented_image = centers[labels.flatten()]
-    segmented_image = segmented_image.reshape(image.shape)
-    
-    # disable only the cluster number 2 (turn the pixel into black)
-    masked_image = np.copy(image)
-    # convert to the shape of a vector of pixel values
-    masked_image = masked_image.reshape((-1, 1))
-    # color (i.e cluster) to disable
-    cluster = 2
-    masked_image[labels != cluster] = [0]
-    # convert back to original shape
-    masked_image = masked_image.reshape(image.shape)
-    
-    return masked_image
-   
-    
-    image = cv2.medianBlur(image,3)
-    #image = np.invert(image)
-    return cv2.threshold(image, 140, 255, cv2.THRESH_BINARY)[1]
-    return image
-def thresholding2(image):
-    
-    image = cv2.medianBlur(image,7)
-    #image = np.invert(image)
-    return image
- 
-def crop(image):
-    top=314
-    left=230
-    height= 150
-    width=400
-    return image[top : (top + height) , left: (left + width)]
-
-
-
-camera = PiCamera(
-    resolution=(800, 600)
-)
-camera.awb_mode = 'off'
-camera.awb_gains = (1.5, 2.0)
-camera.shutter_speed = 60000
-camera.iso = 800
-while True:
-    file = "/home/pi/solar-monitor/camera/" + str(datetime.now()) + ".jpg"
-    camera.capture(file)
-    print("Captured.")
-    img = cv2.imread(file)
-    img = crop(img)
-    img = thresholding2(img)
-    print(parse(img))
-    cv2.imwrite(file + '_r.jpg', img)
-    time.sleep(2)

+ 0 - 79
data-writer.py

@@ -1,79 +0,0 @@
-import time
-import random
-from influxdb import InfluxDBClient
-client = InfluxDBClient(host="localhost", port=8086, username="influxdb", password="influxdbTSGAMES")
-client.create_database("influxdb")
-client.switch_database('influxdb')
-#!/usr/bin/env python3
-
-import argparse
-import signal
-import sys
-import time
-import logging
-
-from rpi_rf import RFDevice
-VOLTAGE = 0
-AMPERE = 1
-
-# min samples in a row stable to be accepted as new value
-STABLE_COUNT = 3
-# max diff which is considered stable between new values
-STABLE_DIFF_THRESHOLD = .1
-rfdevice = None
-
-# pylint: disable=unused-argument
-def exithandler(signal, frame):
-    rfdevice.cleanup()
-    sys.exit(0)
-
-logging.basicConfig(level=logging.INFO)
-
-parser = argparse.ArgumentParser(description='Receives a decimal code via a 433/315MHz GPIO device')
-parser.add_argument('-g', dest='gpio', type=int, default=27,
-                    help="GPIO pin (Default: 27)")
-args = parser.parse_args()
-
-signal.signal(signal.SIGINT, exithandler)
-rfdevice = RFDevice(args.gpio, rx_tolerance=80)
-rfdevice.enable_rx()
-timestamp = None
-logging.info("Listening for codes on GPIO " + str(args.gpio))
-previous_values = {}
-counts = {}
-while True:
-    if rfdevice.rx_code_timestamp != timestamp:
-        timestamp = rfdevice.rx_code_timestamp
-        code = rfdevice.rx_code
-        codeType = code & 0xff
-        value = (code >> 8) / 1000.
-        U = 0
-        I = 0
-        if value:
-            if codeType in previous_values and abs(previous_values[codeType]-value) <= STABLE_DIFF_THRESHOLD:
-                counts[codeType] += 1
-            else:
-                counts[codeType] = 0
-            previous_values[codeType] = value
-        logging.info("Data Received: " + str(code) + "/" + str(codeType) + " / " + str(value))
-        if codeType in counts and counts[codeType] >= STABLE_COUNT:
-            if codeType == VOLTAGE or True:
-                U = value
-                logging.info("Stable Voltage:" +str(U))
-                client.write_points([
-                    {
-                        "measurement": "solar",
-                        "tags": {
-                            "type": "U"
-                        },
-                        "fields": {
-                            "value": U
-                        }
-                    }
-                ], time_precision='ms')
-            if codeType == AMPERE:
-                logging.info("Ampere")
-                I = value
-            
-    time.sleep(0.05)
-rfdevice.cleanup()

+ 2 - 2
docker-compose.yml

@@ -16,7 +16,7 @@ services:
         - grafana-data:/var/lib/grafana
         
   influxdb:
-    #image: influxdb:2.0
+    #image: influxdb:1.8
     image: arm32v7/influxdb:latest
     container_name: influxdb
     ports:
@@ -32,7 +32,7 @@ services:
       DOCKER_INFLUXDB_INIT_ORG: raspberry
       DOCKER_INFLUXDB_INIT_BUCKET: influxdb
       DOCKER_INFLUXDB_INIT_ADMIN_TOKEN: influxdbTSGAMES
-      DOCKER_INFLUXDB_INIT_RETENTION: 1h
+      DOCKER_INFLUXDB_INIT_RETENTION: 2y
     volumes:
       # Data persistency
       # sudo mkdir -p /srv/docker/influxdb/data

+ 157 - 0
src/data-writer-opencv.py

@@ -0,0 +1,157 @@
+import cv2
+import pytesseract
+import time
+from datetime import datetime
+from pytesseract import Output
+
+import cv2
+import pytesseract
+from pytesseract import Output
+import numpy as np
+import re
+import os
+from influxdb import InfluxDBClient
+client = InfluxDBClient(host="influxdb", port=8086, username="influxdb", password="influxdbTSGAMES")
+client.create_database("influxdb")
+client.switch_database('influxdb')
+class SolarMonitor:
+    camera = None
+    def __init__(self, test = False):
+        self.test = test
+        if not test:
+            self.initCamera()
+        
+    def parse(self, img): 
+        custom_oem=r'--oem 3 --psm 11'
+        # https://github.com/adrianlazaro8/Tesseract_sevenSegmentsLetsGoDigital
+        data = pytesseract.image_to_data(img, lang='lets', 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)
+        if len(results) == 2:
+            results = list(map(lambda x: int(x) / 10.,results ))
+            if results[0] < 100 and results[1] < 1000:
+                return results
+        return None
+    def thresholding(self, image):
+        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
+        
+        pixel_values = image.reshape((-1, 1))
+        pixel_values = np.float32(pixel_values)
+        print(pixel_values.shape)
+        # define stopping criteria
+        criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.2)
+        # number of clusters (K)
+        k = 3
+        _, labels, (centers) = cv2.kmeans(pixel_values, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
+        # convert back to 8 bit values
+        centers = np.uint8(centers)
+
+        # flatten the labels array
+        labels = labels.flatten()
+        # convert all pixels to the color of the centroids
+        segmented_image = centers[labels.flatten()]
+        segmented_image = segmented_image.reshape(image.shape)
+        
+        # disable only the cluster number 2 (turn the pixel into black)
+        masked_image = np.copy(image)
+        # convert to the shape of a vector of pixel values
+        masked_image = masked_image.reshape((-1, 1))
+        # color (i.e cluster) to disable
+        cluster = 2
+        masked_image[labels != cluster] = [0]
+        # convert back to original shape
+        masked_image = masked_image.reshape(image.shape)
+        
+        return masked_image
+    
+        
+        image = cv2.medianBlur(image,3)
+        #image = np.invert(image)
+        return cv2.threshold(image, 140, 255, cv2.THRESH_BINARY)[1]
+        return image
+    def thresholding2(self, image):
+        
+        image = cv2.medianBlur(image,7)
+        #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)]
+
+
+
+    def initCamera(self):
+        from picamera import PiCamera
+        self.camera = PiCamera(
+            resolution=(800, 600)
+        )
+        self.camera.awb_mode = 'off'
+        self.camera.awb_gains = (1.5, 2.0)
+        self.camera.shutter_speed = 60000
+        self.camera.iso = 800
+
+    def capture(self): 
+        file = "/home/pi/solar-monitor/camera/" + str(datetime.now()) + ".jpg"
+        self.camera.capture(file)
+        print("Captured.")
+        img = cv2.imread(file)
+        img = self.crop(img)
+        return [file, img]
+
+    def writeData(self, results): 
+        client.write_points([
+            {
+                "measurement": "solar",
+                "tags": {
+                    "type": "U"
+                },
+                "fields": {
+                    "value": results[0]
+                }
+            },
+                                {
+                "measurement": "solar",
+                "tags": {
+                    "type": "W"
+                },
+                "fields": {
+                    "value": results[1]
+                }
+            }
+        ], time_precision='ms')
+        
+    def run(self):
+        if self.test:
+            for file in os.listdir('tests'):
+                img = cv2.imread('tests/' + file)
+                img = self.thresholding2(img)
+                results = self.parse(img)
+                print(file)
+                print(results)
+                if results:
+                    self.writeData(results)
+                cv2.imwrite(file + '_r.jpg', img)
+        else:
+            [file, img] = solar.capture()
+            img = self.thresholding2(img)
+            results = self.parse(img)
+            print(results)
+            if results:
+                self.writeData(results)
+            cv2.imwrite(file + '_r.jpg', img)
+            # os.unlink(file)
+            time.sleep(2)    
+
+while True:
+    solar = SolarMonitor(test = True)
+    solar.run()
+    

+ 6 - 0
src/entrypoint.sh

@@ -0,0 +1,6 @@
+#!/bin/bash
+
+ls -alh
+/app/wait-for-it.sh -t 0 influxdb:8086
+
+python3 data-writer-opencv.py

+ 1 - 1
src/requirements.txt

@@ -1,4 +1,4 @@
-influxdb-client # this is for 2.x
+#influxdb-client # this is for 2.x
 influxdb
 rpi-rf
 opencv-python==4.5.3.56

+ 0 - 71
src/test-cv.py

@@ -1,71 +0,0 @@
-import cv2
-import pytesseract
-from pytesseract import Output
-import numpy as np
-import re
-def parse(image): 
-    custom_oem=r'--oem 3 --psm 11'
-    # https://github.com/adrianlazaro8/Tesseract_sevenSegmentsLetsGoDigital
-    data = pytesseract.image_to_data(img, lang='lets', 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)
-    if len(results) == 2:
-        results = list(map(lambda x: int(x) / 10.,results ))
-        if results[0] < 100 and results[1] < 1000:
-            return results
-    return None
-def thresholding(image):
-    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
-    
-    pixel_values = image.reshape((-1, 1))
-    pixel_values = np.float32(pixel_values)
-    print(pixel_values.shape)
-    # define stopping criteria
-    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.2)
-    # number of clusters (K)
-    k = 3
-    _, labels, (centers) = cv2.kmeans(pixel_values, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
-    # convert back to 8 bit values
-    centers = np.uint8(centers)
-
-    # flatten the labels array
-    labels = labels.flatten()
-    # convert all pixels to the color of the centroids
-    segmented_image = centers[labels.flatten()]
-    segmented_image = segmented_image.reshape(image.shape)
-    
-    # disable only the cluster number 2 (turn the pixel into black)
-    masked_image = np.copy(image)
-    # convert to the shape of a vector of pixel values
-    masked_image = masked_image.reshape((-1, 1))
-    # color (i.e cluster) to disable
-    cluster = 2
-    masked_image[labels != cluster] = [0]
-    # convert back to original shape
-    masked_image = masked_image.reshape(image.shape)
-    
-    return masked_image
-   
-    
-    image = cv2.medianBlur(image,3)
-    #image = np.invert(image)
-    return cv2.threshold(image, 140, 255, cv2.THRESH_BINARY)[1]
-    return image
-def thresholding2(image):
-    
-    image = cv2.medianBlur(image,7)
-    #image = np.invert(image)
-    return image
- 
-for file in ['test4.jpg', 'test3.jpg', 'test2.jpg', 'test1.jpg']:
-    print(file)
-    img = cv2.imread(file)
-    img = thresholding2(img)
-    print(parse(img))
-    cv2.imwrite(file + '_r.jpg', img)
-

+ 0 - 27
src/test.py

@@ -1,27 +0,0 @@
-import easyocr
-import cv2
-import pytesseract
-import numpy as np
-
-def thresholding(image):
-    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
-    image = cv2.medianBlur(image,3)
-    brightness = 150
-    contrast = 3.
-    brightness += int(round(255*(1-contrast)/2))
-    image = cv2.addWeighted(image, contrast, image, 0, brightness)
-    #image = np.invert(image)
-    return cv2.threshold(image, 140, 255, cv2.THRESH_BINARY)[1]
-    return image
- 
-img_source = cv2.imread('DSC_0099_r4.JPG')
-img = thresholding(img_source)
-cv2.imwrite('result.jpg', img)
-
-custom_oem=r'--oem 3 --psm 11 -c tessedit_char_whitelist=0123456789'
-d = pytesseract.image_to_data(img) #, lang='letsgodigital', config=custom_oem, output_type=Output.DICT)
-print(d)
-
-reader = easyocr.Reader(['en'], gpu=False)
-result = reader.readtext('result.jpg', allowlist='0123456789')
-print(result)

+ 0 - 10
src/test2.py

@@ -1,10 +0,0 @@
-from doctr.models import ocr_predictor
-from doctr.io import DocumentFile
-# https://pypi.org/project/python-doctr/
-model = ocr_predictor(det_arch='db_resnet50', reco_arch='crnn_vgg16_bn', pretrained=True)
-# Image
-single_img_doc = DocumentFile.from_images("result.jpg")
-
-result = model(single_img_doc)
-
-print(result)