# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + [markdown] nbpresent={"id": "eff3a02c-cf13-4c08-b2b3-df48c676159c"}
# <head>
# <meta content="text/html; charset=ISO-8859-1"
# http-equiv="content-type">
# <title></title>
# </head>
# <body>
# <p style="text-align: center;" class="MsoNormal"><span
# style="font-weight: bold; text-decoration: underline;">Hackathon 2018 - chart-digitizer</span></p>
#
# <p class="MsoNormal"><br>
# <o:p></o:p></p>
# <p style="text-align: center;" class="MsoNormal"><o:p></o:p></p>
# <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
# <p style="text-align: center;" class="MsoNormal"><span
# style="font-style: italic;"><NAME></span><o:p></o:p></p>
# <p style="text-align: center;" class="MsoNormal"><span
# style="font-style: italic;"><NAME></span><o:p></o:p></p>
# <p style="text-align: center;" class="MsoNormal"><span
# style="font-style: italic;"><NAME></span><o:p></o:p></p>
# <p style="text-align: center;" class="MsoNormal"><span
# style="font-style: italic;"><NAME></span><o:p></o:p></p>
# <br>
# <p class="MsoNormal"><o:p></o:p></p>
# </body>
#
# </div><!--/.row-->
#  <p class="clearfix"></p>
# <br />			
# <div class="row">	

# + [markdown] nbpresent={"id": "15f3fbd2-0e7f-4ceb-93c1-8936cdebed7b"}
# <head>
# <meta content="text/html; charset=ISO-8859-1"
# http-equiv="content-type">
# <title></title>
# </head>
# <body>
# <p style="text-align: center;" class="MsoNormal"><img
# style="width: 50px; height: 15px;" alt="Python"
# src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f8/Python_logo_and_wordmark.svg/260px-Python_logo_and_wordmark.svg.png"><br>
# <o:p></o:p></p>
# <p style="text-align: center;" class="MsoNormal"><img alt="OpenCV"
# src="https://a.fsdn.com/allura/p/opencvlibrary/icon" /><br>
# <span style="font-weight: bold; text-decoration: underline;"></span><o:p></o:p></p><p class="MsoNormal"><img style="width: 50px; height: 15px;"
# alt="Anaconda"
# src="https://upload.wikimedia.org/wikipedia/en/thumb/c/cd/Anaconda_Logo.png/200px-Anaconda_Logo.png"><br>
# <o:p></o:p></p>
# </body>

# + [markdown] nbpresent={"id": "7fb97c5e-4092-4e4e-8023-b795f49791da"}
#
#    - Random chart generator
#    - Model training using object detection
#    - Result analysis
#

# + nbpresent={"id": "e0937ba9-ac52-4500-9355-f809320da00f"}
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib

# + [markdown] nbpresent={"id": "912cb702-4e13-45e3-be96-8ca5bb075111"}
# Random bar chart generator

# +

data = pd.DataFrame(data=np.random.rand(5,1), index=range(1,6), columns=['Fred'])
m,n = np.shape(data)

plt.clf()
plt.bar(x=data.index.values, height=data.values.ravel(), color='k') # figsize=(10, 6))
# Options for later from https://matplotlib.org/api/_as_gen/matplotlib.pyplot.bar.html
# bar_width = 0.35
# alpha = .3
fig=plt.gcf()
fig.set_size_inches(3, 2)
plt.axis('off')
fig.tight_layout()
fig.canvas.draw()
# grab the pixel buffer and dump it into a numpy array
pixels = np.array(fig.canvas.renderer._renderer)

plt.plot();
# -

# Display generated chart

# + nbpresent={"id": "b6b7891d-3c45-408b-b1e0-9ff049e33104"}
print(pixels);
print(data);

# + nbpresent={"id": "ae7b3af1-e0c2-4011-8fb6-ae6794c5dfa9"}
y, X = img_gen_bar()
print(y)

#for neural net
X=X/255

#for DNN only
#X=X.reshape(1,-1,3)

#data={}
#for i in range(1000) :
#    data[i] = (generate_bar_chart() )


# + [markdown] nbpresent={"id": "45a597e1-1a3d-4e04-b570-92fb942bbbc8"}
# For historical reasons, OpenCV defaults to BGR format instead of usual RGB <br><br>
# Lets convert OpenCV image to RGB consistently<br>
#
#
# The Lab color space has three components. <br><br>
#
# L – Lightness ( Intensity ).<br>
# a – color component ranging from Green to Magenta.<br>
# b – color component ranging from Blue to Yellow. <br><br>
#
# The Lab color space is quite different from the RGB color space. In RGB color space the color information is separated into three channels but the same three channels also encode brightness information. On the other hand, in Lab color space, the L channel is independent of color information and encodes brightness only. The other two channels encode color.

# + nbpresent={"id": "dfa4b5ff-7f56-4a23-84b4-af0ac0e890ca"}
cvimrgb = cv2.cvtColor(cvim2disp,cv2.COLOR_BGR2RGB)

#or
#imbgr = cv2.cvtColor(im2disp,cv2.COLOR_RGB2BGR)

figure()
imshow(cvimrgb)

cvimlab = cv2.cvtColor(cvim2disp,cv2.COLOR_BGR2LAB)

#or
#imbgr = cv2.cvtColor(im2disp,cv2.COLOR_RGB2BGR)

figure()
imshow(cvimlab)


# + [markdown] nbpresent={"id": "5e5ef64d-692e-4935-bcf8-8aefff52c384"}
# Useful utility function

# + nbpresent={"id": "71e23afe-a5a6-4888-8fbb-574af66c9ed1"}
img = cv2.imread('sample-1.png', 0)
img = cv2.threshold(img, 100, 255, cv2.THRESH_BINARY)[1]  # ensure binary
ret, labels = cv2.connectedComponents(img)

# Map component labels to hue val
label_hue = np.uint8(179*labels/np.max(labels))
blank_ch = 255*np.ones_like(label_hue)
labeled_img = cv2.merge([label_hue, blank_ch, blank_ch])

# cvt to BGR for display
labeled_img = cv2.cvtColor(labeled_img, cv2.COLOR_HSV2BGR)

# set bg label to black
labeled_img[label_hue==0] = 0

figure()
imshow( labeled_img)
# -

# Simple filtering example

# +
im2disp = imread('sample-1.png')

blurred = cv2.GaussianBlur(im2disp,(19,19),0)

figure()
imshow(blurred)


#more general method
kernel = np.ones((5,5),np.float32)/25
blurred2 = cv2.filter2D(im2disp,-1,kernel)

figure()
imshow(blurred2)


# + [markdown] nbpresent={"id": "0b2b252f-dc8d-46b3-865c-d91e5a67ed6a"}
# Converting to LAB 

# + nbpresent={"id": "b580a43e-03cd-4fdc-8f0d-e69653969251"}
cv2.imwrite('data/mycvimage.png', cvim2disp)
#or
imsave('data/myimage.png',im2disp)

# -

x=2
# %whos

# + [markdown] nbpresent={"id": "1501269c-2107-40a8-a8ab-d46443c5c133"}
# #1 numpy gotcha for people coming from Matlab

# + nbpresent={"id": "1aee9993-9a16-4053-bfc8-e97ea1fabbdb"}
x = zeros(5)
y = x

y[1] = 1

#uncomment next line and run
print(x)


# + [markdown] nbpresent={"id": "0f618461-e7ba-41e3-9416-e70fd18e03d5"}
# What happened?  Why did modifying y change x?
# <br><br>A: Python copies arrays and other mutable data types by reference by default
# <br><br><br>Here's what you probably want:
#

# + nbpresent={"id": "285b7d68-b953-4b9e-8095-9e28a454adee"}
x=zeros(5)
y=x.copy()

y[1] = 1

print(x)


# + [markdown] nbpresent={"id": "fdf28f4d-e257-49f5-a7c3-a4050163da8b"}
# Let's run some of the included OpenCV examples
#

# + nbpresent={"id": "48cf29b7-5967-4798-acf8-12fcb8c08928"}
# %run inpaint.py


# + nbpresent={"id": "c2b0183e-eb4e-43fe-ad48-4023b9bb7409"}
# %run deconvolution.py


# + nbpresent={"id": "755b4867-41e4-4c84-94bc-c91926f4de4e"}
# %run find_obj.py


# + nbpresent={"id": "5525c670-90ca-440a-8fdf-3e4583db70d7"}
# %run peopledetect.py

# -

# cd python

StarcoderdataJupyterScriptsDedupFiltered