Thursday, February 13, 2020

Project #8 - Stock Tracking Graph

For my 8th project, I made a program that pulls stock data from Yahoo Finance and displays it in a graph. For this example, I chose GOOG which is Googles stock sticker. The data was able to be gathered from Yahoo using the pandas_datareader and the graph was generated using bokeh.plotting.

from pandas_datareader import data
import datetime
from bokeh.plotting import figure, show, output_file

start = datetime.datetime(2015, 11, 1)
end = datetime.datetime(2016, 3, 15)

df = data.DataReader(name="GOOG", data_source="yahoo", start=start, end=end)
date_increase = df.index[df.Close > df.Open]
date_decrease = df.index[df.Close < df.Open]


def inc_dec(c, o):
    if c > o:
        value = "Increase"    elif c < o:
        value = "Decrease"    else:
        value = "Equal"    return value


df["Status"] = [inc_dec(c, o) for c, o in zip(df.Close, df.Open)]
df["Middle"] = (df.Open + df.Close0)/2df["Height"] = abs(df.Close - df.Open)

p = figure(x_axis_type='datetime', width=1000, height=1000, sizing_mode="scale_width")
p.title = "Candlestick Chart"p.grid.grid_line_alpha = 0.3
hours_12 = 12*60*60*1000
p.segment(df.index, df.High, df.index, df.low, color="black")

p.rect(df.index[df.Status == "Increase"], df.Middle(df.Status == "Increase"),       
hours_12, df.Height[df.Status == "Increase"], fill_color="#CCFFFF", line_color="black")

p.rect(df.index[df.Status == "Decease"],  df.Middle(df.Status == "Decrease"),      
 hours_12, df.Height[df.Status == "Decrease"], fill_color="#FF3333", line_color="black")

output_file("CS.html")

The graph came out looking like this:




Wednesday, February 12, 2020

Project #7 - Web Scraper

For my 7th project, I made a program that scrapes information from a webpage and shows it in the terminal.

import requests
from bs4 import BeautifulSoup

r = requests.get("http://www.pyclass.com/real-estate/rock-springs-wy/LCWYROCKSPRINGS/", 
headers={'User-agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0'})
c = r.content

soup = BeautifulSoup(c, "html.parser")

all = soup.find_all("div", {"class": "propertyRow"})

all[0].find("h4", {"class": "propPrice"}).text.replace("\n", "").replace(" ", "")


# Pulling data from each listing and printing it in the terminal.
for item in all:
    try:
        print(item.find_all("span", {"class", "propAddressCollapse"})[0].text)
    except:
        print("No Info")

    try:
        print(item.find_all("span", {"class", "propAddressCollapse"})[1].text)
    except:
        print("No Info")

    try:
        print(item.find("h4", {"class", "propPrice"}).text.replace("\n", "").replace(" ", ""))
    except:
        print("No Info")

    try:
        print(item.find("span", {"class", "infoBed"}).find("b").text)
    except:
        print("No Info")

    try:
        print(item.find("span", {"class", "infoValueFullBath"}).find("b").text)
    except:
        print("No Info")

    try:
        print(item.find("span", {"class", "infoValueHalfBath"}).find("b").text)
    except:
        print("No half bath")

    try:
        print(item.find("span", {"class", "infoSqFt"}).find("b").text)
    except:
        print("No Info")

    for column_group in item.find_all("div", {"class": "columnGroup"}):
        for feature_group, feature_name in zip(column_group.find_all("spans", {"class": "featureGroup"}), 
column_group.find_all("span", {"class": "featureName"})):
            print(feature_group.text, feature_name.text)

    print(" ")

Monday, February 10, 2020

Project #6 - Video Motion Tracking

For my 6th project I made a program that tracks motion through a video camera and writes to a graph of when there was motion.

Here is the code for the video motion tracking:
import cv2
import pandas
import time
from datetime import datetime


first_frame = Nonestatus_list = [None, None]
times = []
df = pandas.DataFrame(columns=["Start", "End"])

video = cv2.VideoCapture(0)

while True:

    check, frame = video.read()
    status = 0    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (21, 21), 0)

    if first_frame is None:
        first_frame = gray
        continue
    delta_frame = cv2.absdiff(first_frame, gray)

    thresh_frame = cv2.threshold(delta_frame, 30, 255, cv2.THRESH_BINARY)[1]

    thresh_frame = cv2.dilate(thresh_frame, None, iterations=2)

    (cnts, _) = cv2.findContours(thresh_frame.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    for contour in cnts:
        if cv2.contourArea(contour) < 10000:
            continue        status = 1
        (x, y, w, h) = cv2.boundingRect(contour)
        cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 3)

    status_list.append(status)

    status_list=status_list[-2:]

    if status_list[-1] == 1 and status_list[-2] == 0:
        times.append(datetime.now())

    if status_list[-1] == 0 and status_list[-2] == 1:
        times.append(datetime.now())

    cv2.imshow("Gray Frame", gray)
    cv2.imshow("Delta Frame", delta_frame)
    cv2.imshow("Threshold Frame", thresh_frame)
    cv2.imshow("Color frame", frame)

    key = cv2.waitKey(1)

    print(gray)
    print(delta_frame)

    if key == ord('q'):
        if status == 1:
            times.append(datetime.now())
        break
print(status_list)
print(times)

for i in range(0, len(times), 2):
    df = df.append({"Start": times[i], "End": times[i+1]}, ignore_index=True)

df.to_csv("Times.csv")

video.release()
cv2.destroyAllWindows()


Here is the code for generating the graph:
from motiontracker import df
from bokeh.plotting import figure, show, output_file
from bokeh.models import HoverTool, ColumnDataSource

df["Start_string"] = df["Start"].dt.strftime("%Y-%m-%d %H:%M:%S")
df["End_string"] = df["End"].dt.strftime("%Y-%m-%d %H:%M:%S")

cds = ColumnDataSource(df)

p = figure(x_axis_type='datetime', height=100, width=500, responsive=True, title="Motion Graph")
p.yaxis.minor_tick_color = Nonep.ygrid[0].ticker.desired_num_ticks = 1
hover = HoverTool(tooltips=[("Start", "@Start_string"), ("End", "@End_string")])

q = p.quad(left="Start", right="End", bottom=0, top=1, color="blue", source=cds)

output_file("Graph.html")
show(p)


Wednesday, February 5, 2020

Project #5 - Image Face Finder

For this project I built a simple but smart program that can recognize faces in images. The program uses CV2 and an xml document to distinguish the faces in any image.

import cv2

face_cascade= cv2.CascadeClassifier("haarcascade_frontalface_default.xml")

img = cv2.imread("People.jpg")

gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

faces = face_cascade.detectMultiScale(gray_img, scaleFactor=1.1, minNeighbors=20)

for x, y, w, h in faces:
    img = cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 3)

print(type(faces))
print(faces)

cv2.imshow("Gray", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Here is what the outcome looks like:







Monday, February 3, 2020

Project #4 - OOP Update

I updated the back-end script to conform with the Object Oriented Programming structure.

import sqlite3


class Database:

    def __init__(self, db):
        self.conn = sqlite3.connect("books.db")
        self.cur = self.conn.cursor()
        self.cur.execute("CREATE TABLE IF NOT EXISTS book (id INTEGER PRIMARY KEY, title text author text, 
year integer, isbn intege)")
        self.conn.commit()

    def insert(self, title, author, year, isbn):
        self.cur.execute("INSERT INTO book VALUES (NULL, ?, ?, ?, ?", (title, author, year, isbn))
        self.conn.commit()

    def view(self):
        self.cur.execute("SELECT * FROM book")
        rows = self.cur.fetchall()
        return rows

    def search(self, title="", author="", year="", isbn=""):
        self.cur.execute("SELECT * FROM book WHERE title=? OR author=? OR year=? OR isbn=?", (title, author, year, isbn))
        rows = self.cur.fetchall()
        return rows

    def delete(self, id):
        self.cur.execute("DELETE FROM book WHERE id=?", (id,))
        self.conn.commit()

    def update(self, id, title, author, year, isbn):
        self.cur.execute("UPDATE book SET title=?, author=?, year=?, isbn=?, (id, title, author, year, isbn)")
        self.conn.commit()


def __dil__(self):
    self.conn.close()

Project #4 - Record Searcher

For my 4th project I built a graphical user interface program that stores book information like title, author, year, and IBSN. The user can view all records, search an entry, add entry, update entry, delete, and close.

Here is the front end script of my code:
from tkinter import *
import backend


def get_selected_row(event):
    try:
        index = list1.curselection()[0]
        global selected_tuple
        selected_tuple = list1.get(index)
        e1.delete(0, END)
        e1.insert(END, selected_tuple[1])
        e2.delete(0, END)
        e2.insert(END, selected_tuple[2])
        e3.delete(0, END)
        e3.insert(END, selected_tuple[3])
        e4.delete(0, END)
        e4.insert(END, selected_tuple[4])
    except IndexError:
        pass

def view_command():
    for row in backend.view():
        list1.insert(END, row)


def search_command():
    list1.delete(0, END)
    for row in backend.search(title_text.get(), author_text.get(), year_text.get(), isbn_text.get()):
        list1.insert(END, row)


def add_command():
    backend.insert(title_text.get(), author_text.get(), year_text.get(), isbn_text.get())
    list1.delete(0, END)
    list1.insert(END, (title_text.get(), author_text.get(), year_text.get(), isbn_text.get()))


def delete_command():
    backend.delete(selected_tuple[0])


def update_command():
    backend.update(selected_tuple[0], title_text.get(), author_text.get(), year_text.get(), isbn_text.get())


window = Tk()


window.wm_title("Book Store")


# Labels Section
l1 = Label(window, text="Title")
l1.grid(row=0, column=0)

l2 = Label(window, text="Author")
l2.grid(row=0, column=2)

l3 = Label(window, text="Year")
l3.grid(row=1, column=0)

l4 = Label(window, text="ISBN")
l4.grid(row=1, column=2)


# Text Entry Windows Section
title_text = StringVar()
e1 = Entry(window, textvariable=title_text)
e1.grid(row=0, column=3)

author_text = StringVar()
e2 = Entry(window, textvariable=author_text)
e2.grid(row=1, column=1)

year_text = StringVar()
e3 = Entry(window, textvariable=year_text)
e3.grid(row=0, column=1)

isbn_text = StringVar()
e4 = Entry(window, textvariable=isbn_text)
e4.grid(row=1, column=3)


# Text Box Window
list1 = Listbox(window, height=6, width=35)
list1.grid(row=2, column=0, rowspan=6, columnspan=2)


# Scrollbar
sb1 = Scrollbar(window)
sb1.grid(row=2, column=2, rowspan=6)

list1.configure(yscrollcommand=sb1.set)
sb1.configure(command=list1.yview)

list1.bind('<<ListboxSelect>>', get_selected_row)

# Buttons Section
b1 = Button(window, text="View all", width=12, command=view_command)
b1.grid(row=2, column=3)

b2 = Button(window, text="Search entry", width=12)
b2.grid(row=3, column=3)

b3 = Button(window, text="Add entry", width=12, command=add_command)
b3.grid(row=4, column=3)

b4 = Button(window, text="Update selected", width=12, command=update_command)
b4.grid(row=5, column=3)

b5 = Button(window, text="Delete", width=12, command=delete_command)
b5.grid(row=6, column=3)

b6 = Button(window, text="Close", width=12, command=window.destroy)
b6.grid(row=7, column=3)


window.mainloop()


Here is the back-end script of my code:
import sqlite3


def connect():
    conn = sqlite3.connect("books.db")
    cur = conn.cursor()
    cur.execute("CREATE TABLE IF NOT EXISTS book (id INTEGER PRIMARY KEY, title text author text, 
year integer, isbn intege)")
    conn.commit()
    conn.close()


def insert(title, author, year, isbn):
    conn = sqlite3.connect("books.db")
    cur = conn.cursor()
    cur.execute("INSERT INTO book VALUES (NULL, ?, ?, ?, ?", (title, author, year, isbn))
    conn.commit()
    conn.close()


def view():
    conn = sqlite3.connect("books.db")
    cur = conn.cursor()
    cur.execute("SELECT * FROM book")
    rows = cur.fetchall()
    conn.close()
    return rows


def search(title="", author="", year="",isbn=""):
    conn = sqlite3.connect("books.db")
    cur = conn.cursor()
    cur.execute("SELECT * FROM book WHERE title=? OR author=? OR year=? OR isbn=?", (title, author, year, isbn))
    rows = cur.fetchall()
    conn.close()
    return rows


def delete(id):
    conn = sqlite3.connect("books.db")
    cur = conn.cursor()
    cur.execute("DELETE FROM book WHERE id=?", (id,))
    conn.commit()
    conn.close()


def update(id, title, author, year, isbn):
    conn = sqlite3.connect("books.db")
    cur = conn.cursor()
    cur.execute("UPDATE book SET title=?, author=?, year=?, isbn=?, (id, title, author, year, isbn)")
    conn.commit()
    conn.close()


connect()





Project #8 - Stock Tracking Graph

For my 8th project, I made a program that pulls stock data from Yahoo Finance and displays it in a graph. For this example, I chose GOOG whi...