Friday, 10 February 2023

How to use Python with Telegram bot?

 step1: create a new bot with Telegram botfather

/newbot

test_bot

test_bot

(get the telegram bot token, <telegramBotToken>)


step2: check the new bot chat id, type the following url in the browser, https://api.telegram.org/bot<telegramBotToken>/getUpdates?offset=0

send a message to the new bot in telegram

refresh the browser to get the chat id

(get the telegram bot chat id, <telegramChatID>)


step3: add the bot into a new group and send a message in the group. Then, refresh step2 above

you will see the group chat id, copy and paste into your Python code

 

step4: use a Python code to verify Telegram bot

import requests

TOKEN = "<telegramBotToken>"

chat_id = "<telegramChatID>"

message = "hello from your telegram bot"

url = f"https://api.telegram.org/bot{TOKEN}/sendMessage?chat_id={chat_id}&text={message}"

print(requests.get(url).json()) # this sends the message

 


 

Sunday, 8 January 2023

How to save docker image locally using docker compose and then install it in another server offline?

ref: https://milvus.io/docs/install_offline-docker.md

Download files and images

To install Milvus offline, you need to pull and save all images in an online environment first, and then transfer them to the target host and load them manually.

  1. Download an installation file.
  • For Milvus standalone:
//github.com/milvus-io/milvus/releases/download/v2.2.2/milvus-standalone-docker-compose.yml -O docker-compose.yml
  • For Milvus cluster:
//github.com/milvus-io/milvus/releases/download/v2.2.2/milvus-cluster-docker-compose.yml -O docker-compose.yml
  1. Download requirement and script files.
//raw.githubusercontent.com/milvus-io/milvus/master/deployments/offline/requirements.txt
  1. Pull and save images.
pip3 install -r requirements.txt python3 save_image.py --manifest docker-compose.yml
The images are stored in the /images folder.
  1. Load the images.
cd

Install Milvus offline

Having transferred the images to the target host, run the following command to install Milvus offline.

docker-compose -f docker-compose.yml up -d

Uninstall Milvus

To uninstall Milvus, run the following command.

docker-compose -f docker-compose.yml down


===

requirements.txt:-

docker==5.0.0 nested-lookup==0.2.22

===
save_image.py:-

import argparse import docker import gzip import os import yaml from nested_lookup import nested_lookup if __name__ == "__main__": parser = argparse.ArgumentParser( description="Save Docker images") parser.add_argument("--manifest", required=True, help="Path to the manifest yaml") parser.add_argument("--save_path", type=str, default='images', help='Directory to save images to') arguments = parser.parse_args() with open(arguments.manifest, 'r') as file: template = file.read() images=[] parts = template.split('---') for p in parts: y = yaml.safe_load(p) matches = nested_lookup("image", y) if (len(matches)): images += matches save_path = arguments.save_path if not os.path.isdir(save_path): os.mkdir(save_path) client = docker.from_env() for image_name in set(images): file_name = (image_name.split(':')[0].replace("/", "-")) f = gzip.open(save_path + "/" + file_name + '.tar.gz', 'wb') try: image = client.images.get(image_name) if image.id: print ("docker image \"" + image_name + "\" already exists.") except docker.errors.ImageNotFound: print ("docker pull " + image_name + " ...") image = client.images.pull(image_name) image_tar = image.save(named=True) f.writelines(image_tar) f.close() print("Save docker images to \"" + save_path + "\"")

===
docker-compose.yml:-

version: '3.5' services: etcd: container_name: milvus-etcd image: quay.io/coreos/etcd:v3.5.0 environment: - ETCD_AUTO_COMPACTION_MODE=revision - ETCD_AUTO_COMPACTION_RETENTION=1000 - ETCD_QUOTA_BACKEND_BYTES=4294967296 - ETCD_SNAPSHOT_COUNT=50000 volumes: - ${DOCKER_VOLUME_DIRECTORY:-.}/volumes/etcd:/etcd command: etcd -advertise-client-urls=http://127.0.0.1:2379 -listen-client-urls http://0.0.0.0:2379 --data-dir /etcd minio: container_name: milvus-minio image: minio/minio:RELEASE.2022-03-17T06-34-49Z environment: MINIO_ACCESS_KEY: minioadmin MINIO_SECRET_KEY: minioadmin ports: - "9001:9001" volumes: - ${DOCKER_VOLUME_DIRECTORY:-.}/volumes/minio:/minio_data command: minio server /minio_data --console-address ":9001" healthcheck: test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"] interval: 30s timeout: 20s retries: 3 standalone: container_name: milvus-standalone image: milvusdb/milvus:v2.2.2 command: ["milvus", "run", "standalone"] environment: ETCD_ENDPOINTS: etcd:2379 MINIO_ADDRESS: minio:9000 volumes: - ${DOCKER_VOLUME_DIRECTORY:-.}/volumes/milvus:/var/lib/milvus ports: - "19530:19530" - "9091:9091" depends_on: - "etcd" - "minio" networks: default: name: milvus

Tuesday, 27 December 2022

How to draw a polygon using Opencv + Python?

 import numpy as np

import cv2


# ============================================================================


CANVAS_SIZE = (1080, 1920)


FINAL_LINE_COLOR = (0, 255, 0)

WORKING_LINE_COLOR = (127, 127, 127)


# ============================================================================


class PolygonDrawer(object):

    def __init__(self, window_name):

        self.window_name = window_name # Name for our window


        self.done = False # Flag signalling we're done

        self.current = (0, 0) # Current position, so we can draw the line-in-progress

        self.points = [] # List of points defining our polygon

        self.fpoints = [] # List of points defining our polygon



    def on_mouse(self, event, x, y, buttons, user_param):

        # Mouse callback that gets called for every mouse event (i.e. moving, clicking, etc.)


        if self.done: # Nothing more to do

            return


        if event == cv2.EVENT_MOUSEMOVE:

            # We want to be able to draw the line-in-progress, so update current mouse position

            self.current = (x, y)

        elif event == cv2.EVENT_LBUTTONDOWN:

            # Left click means adding a point at current position to the list of points

            print("Adding point #%d with position(%d,%d)" % (len(self.points), x, y))

            self.points.append((x, y))

            self.fpoints.append([x/CANVAS_SIZE[1], y/CANVAS_SIZE[0]])

        elif event == cv2.EVENT_RBUTTONDOWN:

            # Right click means we're done

            print("Completing polygon with %d points." % len(self.points))

            self.done = True



    def run(self, frame):

        # Let's create our working window and set a mouse callback to handle events

        cv2.namedWindow(self.window_name)

        cv2.imshow(self.window_name, np.zeros(CANVAS_SIZE, np.uint8))

        cv2.waitKey(1)

        cv2.setMouseCallback(self.window_name, self.on_mouse)


        while(not self.done):

            # This is our drawing loop, we just continuously draw new images

            # and show them in the named window

            canvas = frame

            if (len(self.points) > 0):

                # Draw all the current polygon segments

                cv2.polylines(canvas, np.array([self.points]), False, FINAL_LINE_COLOR, 2)

                # And  also show what the current segment would look like

                cv2.line(canvas, self.points[-1], self.current, WORKING_LINE_COLOR)

            # Update the window

            cv2.imshow(self.window_name, canvas)

            # And wait 50ms before next iteration (this will pump window messages meanwhile)

            if cv2.waitKey(50) == 27: # ESC hit

                self.done = True


        # User finised entering the polygon points, so let's make the final drawing

        canvas = np.zeros(CANVAS_SIZE, np.uint8)

        # of a filled polygon

        if (len(self.points) > 0):

            cv2.fillPoly(canvas, np.array([self.points]), FINAL_LINE_COLOR)

        # And show it

        cv2.imshow(self.window_name, canvas)

        # Waiting for the user to press any key

        cv2.waitKey()


        cv2.destroyWindow(self.window_name)

        return canvas


# ============================================================================


if __name__ == "__main__":

    cap = cv2.VideoCapture("test.mp4")

    ret, frame = cap.read()

    if ret == False:

        exit()

    

    pd = PolygonDrawer("Polygon")

    image = pd.run(frame)

    cv2.imwrite("polygon.png", image)

    print("Polygon = %s" % pd.points)

    print("Normalized Polygon = %s" % pd.fpoints)

Tuesday, 20 December 2022

How to build opencv inside a docker to support both python and cpp?

 cmake -DCMAKE_BUILD_TYPE=RELEASE \

        -DCMAKE_INSTALL_PREFIX=/opt/conda/envs/python37/ \

        -DINSTALL_C_EXAMPLES=ON \

        -DOPENCV_GENERATE_PKGCONFIG=ON \

        -DINSTALL_PYTHON_EXAMPLES=ON \

        -DBUILD_SHARED_LIBS=ON \

        -DWITH_TBB=ON \

        -DWITH_V4L=ON \

        -DBUILD_opencv_world=OFF \

        -DOPENCV_PYTHON3_INSTALL_PATH=/opt/conda/envs/python37/lib/python3.7/site-packages/ \

        -DWITH_QT=ON \

        -DWITH_OPENGL=ON \

        -DWITH_FFMPEG=ON \

        -DHAVE_FFMPEG=OFF \

-DWITH_GSTREAMER=ON \

-DHAVE_GSTREAMER=ON \

        -DWITH_CUDA=ON \

        -DHAVE_CUDNN=ON \

        -DCUDNN_INCLUDE_DIRS=/usr/include/x86_64-linux-gnu \

        -DCUDNN_LIBRARY=/usr/lib/x86_64-linux-gnu/libcudnn.so.8.0.4 \

        -DWITH_CUFFT=ON \

        -DWITH_CUBLAS=ON \

        -DWITH_NVCUVID=OFF \

        -DHAVE_NVCUVID=OFF\

        -DWITH_NVCUVENC=ON \

        -DHAVE_NVCUVENC=ON \

        -DBUILD_CUDA_STUBS=OFF \

        -DBUILD_opencv_cudalegacy=ON \

        -DBUILD_opencv_cudacodec=ON \

        -DCUDA_FAST_MATH=ON \

        -DCUDA_ARCH_BIN="8.6" \

        -DCUDA_TOOLKIT_ROOT_DIR="/usr/local/cuda-11.1" \

        -DOPENCV_EXTRA_MODULES_PATH="/data/opencv_contrib-4.4.0/modules" \

        -DBUILD_NEW_PYTHON_SUPPORT=ON \

        -DBUILD_opencv_python3=ON \

        -DHAVE_opencv_python3=ON \

        -DPYTHON3_EXECUTABLE=/opt/conda/envs/python37/bin/python \

        -DPYTHON3_DEFAULT_EXECUTABLE=/opt/conda/envs/python37/bin/python \

        -DPYTHON3_INCLUDE_PATH=/opt/conda/envs/python37/include/python3.7m \

        -DPYTHON3_NUMPY_INCLUDE_DIRS=/opt/conda/envs/python37/lib/python3.7/site-packages/numpy/core/include \

        -DPYTHON3_PACKAGES_PATH=/opt/conda/envs/python37/lib/python3.7/site-packages/ \

        -DPYTHON3_LIBRARIES=/opt/conda/envs/python37/lib/libpython3.7m.so \

        -DPYTHON3_LIBRARIES_PATH=/opt/conda/envs/python37/lib \

-D OpenGL_GL_PREFERENCE=GLVND \

-D BUILD_opencv_rgbd=OFF \

        -D WITH_QT=ON \

        -D WITH_OPENGL=ON \

        -D WITH_GTK_2_X=ON \

        -DBUILD_EXAMPLES=OFF ..

Thursday, 8 December 2022

Wednesday, 7 December 2022

How to capture a single image from a rtsp link?

ffmpeg -rtsp_transport tcp -y -i rtsp://localhost:8554/abc -vframes 1 /data/images/1.jpg 

Tuesday, 6 December 2022

How to use Kafka to send and receive image using python and localhost?

Prerequisites:-

https://hevodata.com/blog/how-to-install-kafka-on-ubuntu/


pip install kafka-python


Producer code

import time
import random
from datetime import datetime
from kafka import KafkaProducer
import cv2
import json
import time
import numpy as np

ktf_host = "localhost:9092"
# Kafka Producer
producer = KafkaProducer(
bootstrap_servers=[ktf_host],
api_version=(0,10,1)
)

if __name__ == '__main__':
# Infinite loop - runs until you kill the program
image = cv2.imread("small.png")
print("image.shape: ", image.shape)
ret, buffer = cv2.imencode('.jpg', image)
while True:
# Send it to our 'messages' topic
print(f'Producing image @ {image.shape}')
t1 = time.time()
jtmp2 = {"data2": "test2"}
jdata = b"!@#$".join([buffer.tobytes(), json.dumps(jtmp2).encode("utf-8")])
producer.send('messages', jdata)
jdata = ""
t2 = time.time()
print("elapsed time (ms): ", (t2-t1)*1000)
# Sleep for a random number of seconds
time_to_sleep = random.randint(1, 3)
time.sleep(time_to_sleep)

consumer code

from io import BytesIO
from PIL import Image
import numpy as np
import cv2
import time
from kafka import KafkaConsumer
import json

if __name__ == '__main__':
# Kafka Consumer
ktf_host = "localhost:9092"
consumer = KafkaConsumer(
'messages',
bootstrap_servers=[ktf_host],
api_version=(0,10,1)
)
for message in consumer:
begin = time.time()
strs = message.value.split(b'!@#$')
jdata = json.loads(strs[1])
print(jdata)
end = time.time()
print("elapsed time (ms): ", (end - begin)*1000)

idata = BytesIO(strs[0])
pil_image = Image.open(idata).convert("RGB")
np_image = np.array(pil_image)
bgr_image = cv2.cvtColor(np_image, cv2.COLOR_RGB2BGR)
rs_image = cv2.resize(bgr_image, (640,640))
print(rs_image.shape)
#cv2.imshow("image", rs_image)
#cv2.waitKey(3)



spawn code:-

import time
import random
from datetime import datetime
from kafka import KafkaProducer
import cv2
import json
import time
import numpy as np
from multiprocessing import Process

ktf_host = "localhost:9092"


def run(camid):
# Kafka Producer
producer = KafkaProducer(
bootstrap_servers=[ktf_host],
api_version=(0,10,1),
buffer_memory=320000000
)
image = cv2.imread("birds.png")
print("image.shape: ", image.shape)
ret, buffer = cv2.imencode('.jpg', image)
while True:
# Send it to our 'messages' topic
# print(f'Producing image @ {image.shape}, {camid}')
t1 = time.time()
jtmp2 = {"data2": camid, "time": str(time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(time.time())))}
print(jtmp2)
jdata = b"!@#$".join([buffer.tobytes(), json.dumps(jtmp2).encode("utf-8")])
producer.send('messages', jdata)
jdata = ""
t2 = time.time()
# print("elapsed time (ms): ", (t2-t1)*1000)
# Sleep for a random number of seconds
time_to_sleep = random.randint(1, 3)
time.sleep(time_to_sleep)

if __name__ == '__main__':
# Infinite loop - runs until you kill the program
# instantiating process with arguments
procs = []
for camid in range(30):
# print(name)
proc = Process(target=run, args=(camid,))
procs.append(proc)
proc.start()

# complete the processes
for proc in procs:
proc.join()