analitics

Pages

Saturday, April 28, 2018

Python 3.6.4 : Testing OpenCV default Hough Line Transform.

This tutorial is about Hough Line Transform and OpenCV python module.
This can be a good example for Hough Line Transform.
See the source code:
import cv2
import numpy as np
img = cv2.imread('test_lines.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# filter black and gray pixels
thresh = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY_INV)[1]

# find lines
lines = cv2.HoughLinesP(thresh, 1, np.pi/180,360,18)

# output lines onto image
for line in lines:
    x1,y1,x2,y2 = line[0]
    cv2.line(img,(x1,y1),(x2,y2),(255,255,0),2)

# show image
cv2.imshow('threshold houghlines', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
This is the result for test_lines.jpg .

You can test by make changes into this line of code:
lines = cv2.HoughLinesP(thresh, 1, np.pi/180,360,18)
According to documentation, the changes are influenced by the range parameters.

Friday, April 27, 2018

Python 3.6.4 : Testing the wit python module .

Today I tested the wit python module.
This python module is a Python library for Wit.ai
You can use for this issues:
  • Bots
  • Mobile apps
  • Home automation
  • Wearable devices
  • Robots
These support languages like:
Afrikaans, Albanian, Arabic, Azerbaijani, Bengali, Bosnian, Bulgarian, Burmese, Catalan, Central Khmer, Chinese, Croatian, Czech, Danish, Dutch, English, Estonian, Finnish, French, Georgian, German, Greek, Hausa, Hebrew, Hindi, Hungarian, Icelandic, Igbo, Indonesian, Inuktitut, Italian, Japanese, Kannada, Kinyarwanda, Korean, Latin, Latvian, Lithuanian, Macedonian, Malay, Maori, Mongolian, Norwegian, Persian, Polish, Portuguese, Romanian, Russian, Serbian, Slovak, Slovenian, Somali, Southern Ndebele, Southern Sotho, Spanish, Swahili, Swati, Swedish, Tagalog, Tamil, Thai, Tsonga, Tswana, Turkish, Ukrainian, Urdu, Uzbek, Venda, Vietnamese, Xhosa, Yoruba and Zulu.
About Wit is free, including for commercial use. So both private and public Wit apps are free and are governed our terms.
For this tutorial I use python 3.6.4, see :
C:\Python364>python.exe
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)]on win32
Type "help", "copyright", "credits" or "license" for more information.
The install of wit python module is simple:
C:\Python364>cd Scripts

C:\Python364\Scripts>pip install wit
Collecting wit
...
Successfully built wit
Installing collected packages: wit
Successfully installed wit-5.1.0

C:\Python364\Scripts>python
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)]
 on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from wit import Wit
>>> w=Wit('3ODKKNB---------')
>>> w.message('Python este un limbaj de programare')
{'_text': 'Python este un limbaj de programare', 'entities': {}, 'msg_id': '0pNT
QXn87P3MYvqmR'}
>>> dir(w)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__form
at__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_s
ubclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__',
 '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclas
shook__', '__weakref__', '_sessions', 'access_token', 'interactive', 'logger', '
message', 'speech']
>>> file = open('C:\Python364\hello_world.wav', 'rb')
>>> w.speech(file)
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Python364\lib\site-packages\wit\wit.py", line 88, in speech
    data=audio_file, headers=headers)
  File "C:\Python364\lib\site-packages\wit\wit.py", line 41, in req
    ' (' + rsp.reason + ')')
wit.wit.WitError: Wit responded with status: 400 (Bad Request)
The error has an open issue.
It does not seem to work properly.
There are some open issues for this python module.
The examples on the internet are not very concise with how to use this python module.

Thursday, April 26, 2018

Python Qt5 : menu example.

This simple tutorial is about PyQt5 and menu window example.
I have a similar example with Qt4 on this blog.
The main reason for this tutorial comes from the idea of simplicity and reuse the source code from PyQt4 and PyQt5.
I do not know if there are significant changes to the Qt5 base IU. However, it is good to check on the official pages. Let's look at the example with comments specific to source code lines:
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 26 17:20:02 2018

@author: catafest
"""
import sys
from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication, QDesktopWidget
from PyQt5.QtGui import QIcon

class Example(QMainWindow):
    #init the example class to draw the window application    
    def __init__(self):
        super().__init__()    
        self.initUI()
    #create the def center to select the center of the screen         
    def center(self):
        # geometry of the main window
        qr = self.frameGeometry()
        # center point of screen
        cp = QDesktopWidget().availableGeometry().center()
        # move rectangle's center point to screen's center point
        qr.moveCenter(cp)
        # top left of rectangle becomes top left of window centering it
        self.move(qr.topLeft())
    #create the init UI to draw the application
    def initUI(self):               
        #create the action for the exit application with shortcut and icon
        #you can add new action for File menu and any actions you need
        exitAct = QAction(QIcon('exit.png'), '&Exit', self)        
        exitAct.setShortcut('Ctrl+Q')
        exitAct.setStatusTip('Exit application')
        exitAct.triggered.connect(qApp.quit)
        #create the status bar for menu 
        self.statusBar()
        #create the menu with the text File , add the exit action 
        #you can add many items on menu with actions for each item
        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(exitAct)
        #resize the window application 
        self.resize(640, 480)
        #draw on center of the screen 
        self.center()
        #add title on windows application 
        self.setWindowTitle('Simple menu')
        #show the application
        self.show()
        #close the UI class
        
if __name__ == '__main__':
    #create the application 
    app = QApplication(sys.argv)
    #use the UI with new  class
    ex = Example()
    #run the UI 
    sys.exit(app.exec_())
The result of this code is this:

Monday, April 2, 2018

The jdoodle online tool for python 3.

This online tool from jdoodle website lets you to programming online with python 3 version.
To see all python modules used by this editor just add this python script and use Execute button.
import sys
import os 
print(help('modules'))

Tuesday, March 20, 2018

Python 3.6.4 : Testing PyQt5 with Spyder I.D.E.

Today I tested the PyQt5 python module with python version 3.6.4.
The script was created and tested with Spyder I.D.E. version 3.2.8.
The PyQt5 version is:
from PyQt5.Qt import PYQT_VERSION_STR
print("PyQt version:", PYQT_VERSION_STR)
PyQt version: 5.9.2
This is the python script:
import sys
from PyQt5.QtWidgets import (QWidget, QCalendarWidget,
    QLabel, QApplication)
from PyQt5.QtCore import QDate
 
class Calendar(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
 
    def initUI(self):
        calendar = QCalendarWidget(self)
        calendar.setGridVisible(True)
        calendar.move(0, 15)
        calendar.clicked[QDate].connect(self.showDate)
        calendar.currentPageChanged[int, int].connect(self.currentPageChanged)
 
        self.lbl = QLabel(self)
        date = calendar.selectedDate()
        self.lbl.setText(date.toString())
        self.lbl.move(0, 0)
 
        self.setGeometry(300, 300, 300, 300)
        self.setWindowTitle('Calendar')
        self.show()
 
    def showDate(self, date):
        self.lbl.setText(date.toString())
 
    def currentPageChanged(self, year, month):
        print(year, month)
 
if __name__ == '__main__':
    app = QApplication(sys.argv)
    calendar_test = Calendar()
    sys.exit(app.exec_())
The result of this script is:

Saturday, March 17, 2018

The Google Cloud SDK - part 003 .

The webapp2 is a lightweight Python web framework compatible with Google App Engine’s.
The webapp2 project, by Rodrigo Moraes, started as a fork of the App Engine web app framework.
The webapp2 includes a number of features such as improved support for URI routing, session management and localization.
You can see google documentation about this python module this link.
They say:
"webapp2 is compatible with the WSGI standard for Python web applications. You don't have to use webapp2 to write Python applications for App Engine. Other web application frameworks, such as Django, work with App Engine, and App Engine supports any Python code that uses the CGI standard. "
This is default start python example from Google Cloud SDK tested in the last tutorial.
import webapp2

class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write('Hello, World!')

app = webapp2.WSGIApplication([
    ('/', MainPage),
], debug=True)
Remember how to deploy your project to google:
C:\Python27>cd python-docs-samples\appengine\standard\hello_world

C:\Python27\python-docs-samples\appengine\standard\hello_world>gcloud app deploy app.yaml
Services to deploy:

descriptor:      [C:\Python27\python-docs-samples\appengine\standard\hello_world\app.yaml]
source:          [C:\Python27\python-docs-samples\appengine\standard\hello_world]
target project:  [xxxxxx]
target service:  [default]
target version:  [xxxxxxxxxxxxx]
target url:      [https://xxxxxx.appspot.com]


Do you want to continue (Y/n)?  y

Beginning deployment of service [default]...
Now I make some changes into main.py file to show you how easy is to use it.
This file: main.py is set into setting file app.yaml like the script: main.app.
Let's make some changes.

The default project is set with --promote true the result is: after a number of uploads you got this error:
ERROR: (gcloud.app.deploy) Error Response: [400] Your app may not have more than 15 versions.
Please delete one of the existing versions before trying to create a new version.
...
To fix go to App Engine - Versions with selected versions and press Delete button.
Then you can make the upload with the command :
gcloud app deploy app.yaml
Also, you can use this:
gcloud app deploy app.yaml --stop-previous-version
Some info about your project can be seen with this command:
gcloud config list
You can see the gcloud versions with:
gcloud version
I tested also with python version 3.6.4 :
C:\Python364\Scripts>pip install webapp2
Requirement already satisfied: webapp2 in c:\python364\lib\site-packages
      2 python-dateutil-2.7.0 setuptools-39.0.0