Farbod pm

import requestsfrom requests.structures import CaseInsensitiveDict
url = “https://map.ir/routes/route/v1/driving/51.421047%2C35.732936%3B51.422185%2C35.731821?alternatives=true&steps=true”
headers = CaseInsensitiveDict()headers[“x-api-key”] = “eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjcyMThjOTliYWM4OWQzNmM4MmQxMzJlNjRjNDE3MWRmNjRjMzJkZjg2OTE5MGE0ZWQyYzU4Njc2M2M1MmI2OTY4ZTcwNDAyM2I1MTQyOWQzIn0.eyJhdWQiOiIxMDgxMCIsImp0aSI6IjcyMThjOTliYWM4OWQzNmM4MmQxMzJlNjRjNDE3MWRmNjRjMzJkZjg2OTE5MGE0ZWQyYzU4Njc2M2M1MmI2OTY4ZTcwNDAyM2I1MTQyOWQzIiwiaWF0IjoxNTk5NzU5NjIwLCJuYmYiOjE1OTk3NTk2MjAsImV4cCI6MTYwMjQ0MTYyMCwic3ViIjoiIiwic2NvcGVzIjpbImJhc2ljIl19.Xzp9yMI5rvT69XHxcEu4w1wqdesAsl01faKxji33XhnUiI-e8AJOQ_8_GzygMXHfZMfrVenNyIiaSMzsdRPN3ug88ogDkVFPx5QLixpQ1Cg0vHPpfaRYaVRpw2vhzIaessR6YS3HGAVyE1IKq3ECgH2a0Q8Lsj-JIggH7r8JHTCIx-WtUMwZKHLlTubiFdxGKsW3VToZj3Y2ldV0s7SlnXtlD6HD1JMui6zuqeRz36qiEIugAQqFL7mxbP-7-n0wuFiuenU5zDJneaSaQ7VJ9c3XUynvrjbtQPl8Aa0shS8YR9XA5aVXwwS0RPIgCbvYoqSCLp44EpA6BvsCRXujdg”

resp = requests.get(url, headers=headers)

Color Detection using HSV image in python,Opencv

Today, we are going to talk about color detection in opencv using python. As you know RGB format is a very regular color format these days. But there are other image color spaces that each one has their own advantages and disadvantages. In this situation , HSV color space would be very useful because of showing the colors. For more information check wikipedia.

Now, we are talking about the code. First of all we have to read the image using imread function. Then changing the color space using cvtColor function and after all some noise reduction using erosion and dilate functions.

import cv2
import numpy as np 
from google.colab.patches import cv2_imshow

img = cv2.imread("/content/JSjbB.png")
img_output = img
frame_hsv = cv2.cvtColor(img_output, cv2.COLOR_BGR2HSV)
frame_hsv_blur = cv2.GaussianBlur(frame_hsv, (7, 7), 0)
lower_values = np.array([130, 100,0])
upper_values = np.array([170,255, 255])
mask = cv2.inRange(frame_hsv_blur, lower_values, upper_values)
kernel = np.ones((5,5),np.uint8)
erosion = cv2.erode(mask,kernel,iterations = 3)
dilation = cv2.dilate(erosion,kernel,iterations = 3)

The result will be some thing like this.

last result

And this would be tha result before erosion and dilate.

Crop image using Opencv in python

Crop image using Opencv in python

In this post we will discuss how to crop an image in python. Opencv convert images to numpy array. That is the trick. You just have to select the locaions you want like below.

img = cv2.imread(file_path)
crop_img = img[0:400, 0:300]

If you read your image from file path and save it to img variable. Then you can save the 400*300 pixels of img into crop_image. 0:400 means from zero pixel to 400 pixel. you can run the below code in colab to practice more.

from google.colab.patches import cv2_imshow
import cv2
file = "/content/YRkw2o9L0XmYHICE467aUSE1Ciy2rdGvkgtc2lUG.png"
img = cv2.imread(file)
crop_img = img[0:400, 0:300] # Crop from {x, y, w, h } => {0, 0, 300, 400}
Originalimage befor applying crop algorithm
Original image
Cropped image after applying crop algorithm

More information

If you are new in opencv with python we recommend you to take a look here.

Resize and Flip images using OpenCV in python

Resizing and fliping codes

Resizing and Flipping images would be very useful when you are working on creating new database for training a CNN, RNN or deep learning algorithms. For example I was working with many images that my Prof captured from wheat plants. We were going to apply CNN algorithm to that images. But the number of the images was too low so I put them in an Folder and ran the below code.

import glob
import cv2
file_path = glob.glob("/content/drive/My Drive/Data/Yellow/*")
for file in file_path:
  img = cv2.imread(file)
  #crop_img = img[0:400, 0:300] # Crop from {x, y, w, h } => {0, 0, 300, 400}
  # perform the actual resizing of the image and show it
  #resized = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)
  img_rotate_90_clockwise = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
  cv2.imwrite(file+'_cv_rotate_90_clockwise.jpg', img_rotate_90_clockwise)
  # True

  img_rotate_90_counterclockwise = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
  cv2.imwrite(file+'_cv_rotate_90_counterclockwise.jpg', img_rotate_90_counterclockwise)
  # True

  img_rotate_180 = cv2.rotate(img, cv2.ROTATE_180)
  cv2.imwrite(file+'_cv_rotate_180.jpg', img_rotate_180)

As you can see I tried to find all of the files path using “glob” library. Next, I tried to read the images using “imread” function. Also trying to resize them using “resize” function. Finally , save the files using “imwrite” function.

More information

Don not forget that you need opencv library to do these stuff. if you are new in image processing go to our instagram page. Also you can see our related pages here.

cv2 import error

This error will happen whenever your python OpenCV is not installed. So you just need to install it using “pip” or make CV2 library using cmake and copy that to your python library folders. Pip is a really good tool to install python packages and you can find a very good tutorial here.

To install OpenCV libarary just run the below in cmd or terminal according to your OS.

>> pip install python_opencv


>>python -m pip install python_opencv

To check that the installation was successful go to pythonshell and import it. no Error should be appear just like below.

How to install libraries/Packages In Python

Pip helps you to install Python libraries. Newest version of python install Pip it’s self but if you lost it try to get the installation file and install Pip.


Open terminal on Ubuntu or CMD in windows. and write this command:

>> pip install <desired package name>


>>python -m pip install <desired package name>

>>python3 -m pip install <desired package name>

The result will be like this if its successful.

Successful installation

Also you can use pip3 install if you want to install a package to your version 3 python.

Remember that if you’re using anaconda you have to go to the conda shell first. Then run the pip command.

>> conda activate <conda_evnvironment >

Some other ways

Sometimes python libraries can not be installed using pip. you have to search it on internet but some of them are like this.

installing pytorch in anaconda

>>conda install -c pytorch pytorch

Error invalid device function

This error may occurs when the gpu setting is wrong. In my experience I have chosen wrong “Generate PTX code” and “Generate GPU code” version. If you are directly running the code on your local device you can check the right version on “properties -> build -> Settings -> cuda” when you see the below pic it means that you have chosen the right version.
** remember clean the project and rebuild it to see the changes.

Feel free and ask your questions or share your experiences with us.

GPU Programming in Ubuntu ,Cuda Installation

General-purpose computing on a graphics processing unit, known as GPU programming, is use of a GPU with CPU to accelerate computation. Today application of GPU programming is image processing, Signal processing, medical imaging and so on. This article show you how to install Cuda platform on your system.

Verify You Have a Supported Version of Linux

First of all you have to determine which distribution and release number you’re running. Type the following at command line:
$ uname -m && cat /etc/*release
you should see output similar to the following, modified for your particular system:
Red Hat Enterprise Linux Workstation release 6.0
The x86_64 line indicates you are running on a 64-bit system.

Verify the System Has gcc Installed

To verify the version of gcc installed on your system, type the following on the command line:
$ gcc –version
Something like below should returned:

if an error message displays , you need to install the development tools from your Linux distribution or obtain a version of gcc and its accompanying toolchain from the web.

Verify the System Has the Correct Kernel Headers and Development Packages Installed

The kernel headers and development packages for the currently running kernel can be installed with:
$ sudo apt-get install kernel-headers-$(uname -r)
$ uname -r returns the version of the kernel your system is running.

Download the NVIDIA CUDA Toolkit

Download your desired installation file from here. After installation verify your file using this code:
$ md5sum <file address>
it takes a while and the result should be like below:

Before installation it’s better to uninstall every other CUDA toolkit using this code:
$ sudo /usr/local/cuda-X.Y/bin/uninstall_cuda_X.Y.pl
and uninstall CUDA driver using below:
$ sudo /usr/bin/nvidia-uninstall
( I did not do it but according to nvidia document you have to do it. Also I am not sure that I had cuda driver or toolkit before.)

Finally, install cuda with code:
$ sudo sh cuda_<version>_linux.run

Matlab logo

Optimization codes in Matlab

A simple optimization problem that has been solved many years ago is the biggest field area with constant surrounding. Also imagine that a carpenter need to build a wooden window like below.

window optimization problem

He need to know with constant timber length, how should he build the vastest window. To determine this problem using Matlab you just need the below code:

clear all;

%window problem

syms r h area(h,r);
area = h*r*2 + pi*r*r/2;%first eq
length = 2*h+2*r+pi*r;%second eq
h = solve( 12== 2*h+2*r+pi*r,h)
solve(diff(subs(area)) ==0)

Here the length of the timers is 12 meters.  “syms” command define the r ,h and area as symbolic variables. Then we define the area and length. In the next step we determine “h” from the second equation. “subs” will substitute the determined h in area and the differential of the result equation should be solved.

Here we discussed a simple optimization problem. Also the Matlab code has discussed. If you had any problem feel free and send me your questions. Thanks!

ربات‌های پرنده شرکت DJI

امروز درباره دنیای ربات‌های پرنده صحبت می‌کنیم. دنیای امروز ما در حال تغییره. هر روزه ربات های زیادی با اهداف مختلف وارد بازار می‌شوند. در صنعت کشاورزی ،صنعت ساختمان ربات های پرنده میخوان خودشون رو جا کنند. برای همینه که ما سعی داریم در اینجا به اونها بپردازیم. در کشور ما،ایران، ربات های پرنده در زمینه فیلم برداری هوایی و امداد و نجات و حتی کشاورزی داره استفاده میشه. استفاده از ربات های سمپاش و صبت اختراع اون در جهان نشونه پیشرفت این ربات در کشور ماست. خب شاید بپرسید که این اطلاعات چه ربطی به شرکت DJI داره. علت این بحث اینه که نیاز استفاده از ربات پرنده در شما ایجاد بشه بعد از اون به سراغ یکی از قوی ترین شرکت های فعال در حوزه ربات های پرنده.

از زمانی که من در زمینه ربات پرنده کار کردم، یعنی چیزی حدود ۶ یا ۷ سال پیش شرکت DJI در زمینه ربات های پرنده بهترین بوده. محصولات این شرکت رو به دو دسته محصولات آماده تجاری که شامل ربات های آماده میشه و محصولات تخصصی که هدفش ساخت ربات های پرنده توسط مهندسین هست دسته بندی کرد. ربات های آماده و هوشمند فانتوم و مویک برای فیلم برداری و نقشه برداری های کوچیک خیلی مناسب هستند. کارباهاشون راحته و به دلیل هوشمندی بالا پرواز کردن با اون ها امنه. نمونه های زیادی از این دو مدل ربات داخل بازار هست که قیمت زیادی دارند، اما به دلیل امنیت بالا خیلی ها تمایل به استفاده ازشون رو دارند.

نوع دیگر ربات ها ربات های پشینهادی شرکت DJI هست که برای هر کاربرد جنس بدنه توان مصرفی موتور هامتفاوته. مثلا برای یک ربات معمولی که بتونه وزن ۱ کیلوگرم رو جابه جا کنه بدنه f450 بهترین گزینه است. این بدنه با ۴ موتور 2212 میتونه علاوه بر وزت ربات یه دوربین یا یه قطعه تا ۱ کیلوگرم رو حمل کنه. برای تفریحات خوبه و حتی برای فیلم برداری. اما خب به دلیل این که هوشمند نیست نیاز به خلبان ماهر داره با یک دوربین خوب و گیمبال مناسب هزینه فیلم برداری تا ۱۰ میلیون میره در صورتی که اگر بخواید از ربات آماده فانتوم استفاده کنین چیزی حدود ۲۰ میلیون باید خرج کنین. البته شاید مدل های دست دوم ربات فانتوم برای شما مقرون به صرفه تر باشه. انتخاب با خود شماست.نمونه های سه موتوره این سری از بدنه با عدد ۳۵۰ و نمونه های شش موتوره اون با سری ۵۵۰ توی بازار شناخته می‌شن.

سری دیگه از ربات های پیشنهادی این شرکت ربات های سری S هستند که ربات S1000 نمونه قدیمی تر اونه و ربات S1200 نمونه جدید تر. این ربات قابلیت این رو داره که چیزی حدود ۱۰ کیلوگرم وزن رو حمل کنه. این پرنده هشت موتوره با موتور های قدرتمندش مخصوص حمل وسایل و تجهیزات بزرگه.