import numpy as np
import cv2
cap=cv2.VideoCapture("./video/10-1.mp4")
kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
bgsubmog=cv2.bgsegm.createBackgroundSubtractorMOG()
cars = []
carno=0
def center(x,y,w,h):
    x1=int(w/2)
    y1=int(h/2)
    cx=x+x1
    cy=y+y1
    return cx,cy

while True:

    ret,frame=cap.read()
    if(ret==True):
        #灰度
        cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
        #去噪（高斯）
        blur=cv2.GaussianBlur(frame,(3,3),5)
        #去背影
        mask=bgsubmog.apply(blur)
        #腐蚀,去掉图中的小方块
        erode=cv2.erode(mask,kernel)
        #膨胀
        dilate=cv2.dilate(erode,kernel,iterations=2)
        #闭操作，去掉物体内部的小块
        close=cv2.morphologyEx(dilate,cv2.MORPH_CLOSE,kernel)
        close = cv2.morphologyEx(close, cv2.MORPH_CLOSE, kernel)
        #查找轮廓
        cnts,h=cv2.findContours(close,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
        cv2.line(frame,(10,600),(1200,600),(255,0,0),3)
        for (i,c) in enumerate(cnts):
            (x,y,w,h)=cv2.boundingRect(c)
            #对车辆的宽和高进行判断，验证是否为有效车辆
            isValid=(w>90)and(h>90)
            if (not isValid):
                continue
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
            cpoint=center(x,y,w,h)
            cars.append(cpoint)
            cv2.circle(frame,(cpoint),5,(0,0,255),-1)
            for (x,y) in cars:
                print(x,y)
                #要有一条线
                if(y>542) and (y<558):
                    carno+=1
                    cars.remove((x,y))
                    print(carno)

        cv2.putText(frame,"cars count:"+str(carno),(500,60),cv2.FONT_HERSHEY_SIMPLEX,2,(255,0,0),5)
        cv2.imshow("video",frame)
        # cv2.imshow("erode",erode)
        # cv2.imshow("dilate", dilate)

    key=cv2.waitKey(1)
    if (key==27):
        break

cap.release()
cv2.destroyAllWindows()

10.车辆识别项目/1.项目实战.py


#!usr/bin/env python
# -*- coding:utf-8 -*-

'''Mask R-CNN coco inference

Using the trained model to detect and segment objects.'''

import os
import sys
import random
import colorsys
import skimage.io

import numpy as np
from skimage.measure import find_contours
import matplotlib.pyplot as plt
from matplotlib import patches
from matplotlib.patches import Polygon
import time

# Root directory of the project
ROOT_DIR = os.path.abspath("../../")

# Import Mask RCNN
sys.path.append(ROOT_DIR)  # To find local version of the library
import mrcnn.model as modellib
from mrcnn.config import Config
from mrcnn import visualize
# Import COCO config
# sys.path.append(os.path.join(ROOT_DIR, "samples/coco/"))  # To find local version
import coco

class InferenceConfig(coco.CocoConfig):
    # Set batch size to 1 since we'll be running inference on
    # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
    GPU_COUNT = 1
    IMAGES_PER_GPU = 1
    # save or not
    SAVE = True

if __name__ == '__main__':

    # set the config and show
    config = InferenceConfig()
    config.display()

    # Local path to trained weights file
    MODEL_DIR = os.path.join(ROOT_DIR, "logs")
    COCO_MODEL_PATH = os.path.join(MODEL_DIR, 'mask_rcnn_coco.h5')

    # Directory of images to run detection on
    IMAGE_DIR = '/home/ason/datasets/images'

    '''Create Model and Load Trained Weights'''

    # Create model object in inference mode.
    model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)

    # Load weights trained on cityscapes
    model.load_weights(COCO_MODEL_PATH, by_name=True)

    # COCO Class names
    # Index of the class in the list is its ID. For example, to get ID of
    # the teddy bear class, use: class_names.index('teddy bear')
    class_names = ['BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane',
                   'bus', 'train', 'truck', 'boat', 'traffic light',
                   'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird',
                   'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear',
                   'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie',
                   'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
                   'kite', 'baseball bat', 'baseball glove', 'skateboard',
                   'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup',
                   'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
                   'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
                   'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed',
                   'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote',
                   'keyboard', 'cell phone', 'microwave', 'oven', 'toaster',
                   'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors',
                   'teddy bear', 'hair drier', 'toothbrush']

    '''Run Object Detection'''

    # get the path of images folder
    file_names = next(os.walk(IMAGE_DIR))[2]
    id = 0 # image id

    # # Run detection
    all_start = time.time()

    for f in file_names:

        # Load images from the images folder
        input_path = os.path.abspath(os.path.join(IMAGE_DIR, f))
        image = skimage.io.imread(input_path)
        print("input_path:", input_path)

        detect = model.detect
        results = detect([image], verbose=1)

        r = results[0]

        # Save and show results
        if config.SAVE == True:
            # output file path
            output_path = input_path.replace("images", "images_results")
            print("output_path:", output_path)
            print('\n')
            visualize.save_instances(output_path = output_path, image=image,
                           boxes=r['rois'], masks=r['masks'], class_ids=r['class_ids'],
                           class_names=class_names, scores=r['scores'], title = "")
        # Show results only
        else:
            visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'],
                                       class_names, r['scores'])

        id += 1

    all_end = time.time()
    average_time = (all_end - all_start) / id
    print("inference runtime per image : {:.3f} s".format(average_time))


# No. 3
# 基于数据构建相关的标签
# 将数据的个个特征打上不同的标记
# 删除缺失值过多的访问（之所以要在这里做而非在之前做，是因为部分访问可能缺失值很多，但是其作为标签时的信息不应被直接浪费）
# 20200715复核
# 20200731复核
from util import *
import os
import numpy as np
import matplotlib.pyplot as plt

def main():
    visit_delete_missing_rate = 0.3
    figure_save_folder = os.path.abspath('../../resource/preprocessed_data/mimic_feature_distribution')
    feature_order_path = os.path.abspath('../../resource/mapping_file/mimic/feature_order.csv')
    data_path = os.path.join(os.path.abspath('../../resource/preprocessed_data/'), 'mimic_after_variable_selection.csv')

    feature_dict_list = read_feature_list(feature_order_path)
    data_dict = general_read_data_to_dict(data_path, skip_extra_line=2)
    print('data, visit: {}, variable num: {}'
          .format(calculate_visit_count(data_dict), calculate_variable_number(data_dict)))
    next_visit_dict = next_visit_index(data_dict)
    reorganized_data_dict = data_reorganization(data_dict, next_visit_dict, feature_dict_list)
    print('reorganized data, visit: {}, variable num: {}'
          .format(calculate_visit_count(reorganized_data_dict), calculate_variable_number(reorganized_data_dict)))
    reorganized_data_dict = delete_visit_missing_too_much(reorganized_data_dict, visit_delete_missing_rate)
    print('discard visit with significant missing, remaining: {}, variable num: {}'
          .format(calculate_visit_count(reorganized_data_dict), calculate_variable_number(reorganized_data_dict)))
    reorganized_data_dict = discard_visit_without_label_or_disease(reorganized_data_dict)
    print('discard visit without label or disease, remaining: {}, variable num: {}'
          .format(calculate_visit_count(reorganized_data_dict), calculate_variable_number(reorganized_data_dict)))

    save_path = os.path.join(os.path.abspath('../../resource/preprocessed_data/'),
                             'mimic_after_label_generate_and_visit_selection.csv')
    write_data_dict_to_csv(reorganized_data_dict, save_path, True, True)
    plot_all_numeric_feature_with_distribution(reorganized_data_dict, figure_save_folder)

def discard_visit_without_label_or_disease(data_dict):
    """20200731复核，删除患者中没有label的或者没有interact的"""
    discard_set = set()
    for patient_id in data_dict:
        for visit_id in data_dict[patient_id]:
            discard_flag_interact = True
            discard_flag_label = True
            for key in data_dict[patient_id][visit_id]:
                if key.__contains__('disease') or key.__contains__('risk') or key.__contains__('category'):
                    value = float(data_dict[patient_id][visit_id][key])
                    if value > 0.5:
                        discard_flag_interact = False
                if key.__contains__('label'):
                    value = float(data_dict[patient_id][visit_id][key])
                    if value > 0.5:
                        discard_flag_label = False
            if discard_flag_interact or discard_flag_label:
                discard_set.add(patient_id+"_"+visit_id)
    for item in discard_set:
        pat_id, visit_id = item.split('_')
        data_dict[pat_id].pop(visit_id)
    print(len(discard_set))
    return data_dict

def read_feature_list(path):
    """
    由于本设计中，数据和知识图谱要搭配使用，因此特征的排列顺序要预先规定，确保idx和KG中的相应节点的idx能够严格对齐
    20200715复核
    20200731复核 确认和知识图谱（无干预版本）排序一致
    """
    feature_dict_list = dict()
    with open(path, 'r', encoding='utf-8-sig', newline='') as file:
        csv_reader = csv.reader(file)
        for line in csv_reader:
            feature_type, name = line
            if not feature_dict_list.__contains__(feature_type):
                feature_dict_list[feature_type] = list()
            feature_dict_list[feature_type].append(name)
    return feature_dict_list

def plot_all_numeric_feature_with_distribution(reorganized_data_dict, save_folder):
    # 绘制数据分布图，为接下来的分布变换做准备
    # 找出数值型变量
    numeric_feature_dict = is_feature_numerical(reorganized_data_dict)
    # 生成序列
    feature_dict = dict()
    for patient_id in reorganized_data_dict:
        for visit_id in reorganized_data_dict[patient_id]:
            for item in reorganized_data_dict[patient_id][visit_id]:
                if numeric_feature_dict[item]:
                    if not feature_dict.__contains__(item):
                        feature_dict[item] = list()
                    value = float(reorganized_data_dict[patient_id][visit_id][item])
                    if value >= 0:
                        feature_dict[item].append(value)
    # 初次值域变换
    num_dict = dict()
    for item in feature_dict:
        feature_dict[item] = np.array(sorted(feature_dict[item]))
        num_dict[item] = {'max': feature_dict[item][-1]+0.001, 'min': feature_dict[item][0]-0.001}
        feature_dict[item] = (feature_dict[item]-num_dict[item]['min'])/(num_dict[item]['max']-num_dict[item]['min'])

    for item in feature_dict:
        skip = feature_dict[item]
        log = np.log(feature_dict[item])
        arcsin = np.arcsin(feature_dict[item])**0.5
        sqrt = feature_dict[item]**0.5
        fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(7, 7))
        axs[0][0].hist(skip, bins=50)
        axs[0][1].hist(log, bins=50)
        axs[1][0].hist(sqrt, bins=50)
        axs[1][1].hist(arcsin, bins=50)
        axs[0][0].set_title('{} skip'.format(item))
        axs[0][1].set_title('{} log'.format(item))
        axs[1][0].set_title('{} sqrt'.format(item))
        axs[1][1].set_title('{} arcsin'.format(item))
        plt.savefig(os.path.join(save_folder, item+'.png'))

def delete_visit_missing_too_much(feature_dict, visit_delete_missing_rate):
    """
    20200715复核
    20200731复核
    如果一个患者的数值型数据的缺失率超过容限，则将该次数据直接删除
    """
    # 构建基本数据，及判定到底哪些特征时数值型
    feature_type_dict = dict()
    for patient_id in feature_dict:
        for visit_id in feature_dict[patient_id]:
            for item in feature_dict[patient_id][visit_id]:
                feature_type_dict[item] = False
    for patient_id in feature_dict:
        for visit_id in feature_dict[patient_id]:
            for item in feature_dict[patient_id][visit_id]:
                value = float(feature_dict[patient_id][visit_id][item])
                if value != 0 and value != 1 and value != -1:
                    feature_type_dict[item] = True
    numerical_feature_num = 0
    for item in feature_type_dict:
        if feature_type_dict[item]:
            numerical_feature_num += 1

    discard_visit_set = set()
    for patient_id in feature_dict:
        for visit_id in feature_dict[patient_id]:
            missing_count = 0
            for item in feature_dict[patient_id][visit_id]:
                if feature_type_dict[item]:
                    value = float(feature_dict[patient_id][visit_id][item])
                    if value < 0:
                        missing_count += 1
            if missing_count / numerical_feature_num > visit_delete_missing_rate:
                discard_visit_set.add(patient_id + '_' + visit_id)

    for item in discard_visit_set:
        patient_id, visit_id = item.split('_')
        feature_dict[patient_id].pop(visit_id)
    return feature_dict

def data_reorganization(data_dict, next_visit_dict, feature_dict_list):
    """
    20200715复核
    20200731复核
    """
    new_data_dict = dict()
    for patient_id in data_dict:
        new_data_dict[patient_id] = dict()
        for visit_id in data_dict[patient_id]:
            # 我们不需要没有标签的数据
            if not (next_visit_dict.__contains__(patient_id) and next_visit_dict[patient_id].__contains__(visit_id)):
                continue
            new_data_dict[patient_id][visit_id] = dict()

            # get label
            next_visit = next_visit_dict[patient_id][visit_id]
            for item in feature_dict_list['disease']:
                if data_dict[patient_id][next_visit].__contains__(item):
                    new_data_dict[patient_id][visit_id][item+'_label'] = data_dict[patient_id][next_visit][item]
            # get risk factor
            for item in feature_dict_list['risk_factor']:
                if data_dict[patient_id][next_visit].__contains__(item):
                    new_data_dict[patient_id][visit_id][item+'_risk_factor'] = data_dict[patient_id][visit_id][item]
            # get treatment
            for item in feature_dict_list['treatment']:
                if data_dict[patient_id][next_visit].__contains__(item):
                    new_data_dict[patient_id][visit_id][item+'_treatment'] = data_dict[patient_id][visit_id][item]
            # get disease
            for item in feature_dict_list['disease']:
                if data_dict[patient_id][next_visit].__contains__(item):
                    new_data_dict[patient_id][visit_id][item+'_disease'] = data_dict[patient_id][visit_id][item]
            # get disease category
            for item in feature_dict_list['category']:
                if data_dict[patient_id][next_visit].__contains__(item):
                    new_data_dict[patient_id][visit_id][item+'_category'] = data_dict[patient_id][visit_id][item]
            # get disease category
            for item in feature_dict_list['feature']:
                if data_dict[patient_id][next_visit].__contains__(item):
                    new_data_dict[patient_id][visit_id][item+'_feature'] = data_dict[patient_id][visit_id][item]
    return new_data_dict

def next_visit_index(data_dict):
    """
    20200715复核
    20200731复核
    """
    index_dict = dict()
    for patient_id in data_dict:
        index_dict[patient_id] = list()
        for visit_id in data_dict[patient_id]:
            index_dict[patient_id].append(int(visit_id))
        index_dict[patient_id] = sorted(index_dict[patient_id])

    next_visit_dict = dict()
    for patient_id in index_dict:
        if len(index_dict[patient_id]) == 1:
            continue
        next_visit_dict[patient_id] = dict()
        for index in range(len(index_dict[patient_id])-1):
            next_visit_dict[patient_id][str(index_dict[patient_id][index])] = str(index_dict[patient_id][index+1])
    return next_visit_dict

if __name__ == '__main__':
    main()


from django.urls import path
from . import views

app_name = 'posts_app'

urlpatterns = [
    path('home/', views.HomeView.as_view(), name='home' ),
	path('signup/', views.UserRegisterView.as_view(), name='signup' ),
    path('create_post/', views.CreatePostView.as_view(), name='create_post' ),
    path('edit_profile/', views.UpdateProfileView.as_view(), name='edit_profile'),
    path('profile/<int:pk>', views.ProfileDetailView.as_view(), name='profile'),
    path('delete_post/<int:pk>', views.DeletePostView.as_view(), name='delete_post'),
    path('like/', views.like, name='like'),
    path('follow/', views.FollowView.as_view(), name='follow'),
    path('detail_post/<int:id>', views.detail_post, name='detail_post'),
    path('edit_post/<int:pk>', views.UpdatePostView.as_view(), name='edit_post'),
    path('validate_username/', views.validate_username, name='validate_username'),

]


from urllib.request import urlopen
from pdfminer.pdfinterp import PDFResourceManager, process_pdf
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from io import StringIO
from io import open

def readPDF(pdfFile):
    rsrcmgr = PDFResourceManager()
    retstr = StringIO()
    laparams = LAParams()
    device = TextConverter(rsrcmgr, retstr, laparams=laparams)

    process_pdf(rsrcmgr, device, pdfFile)
    device.close()

    content = retstr.getvalue()
    retstr.close()
    return content

#pdfFile = urlopen("http://pythonscraping.com/pages/warandpeace/chapter1.pdf");

pdfFile = open("/Users/yanchunyang/Documents/highschools/k9/6.pdf", 'rb')
outputString = readPDF(pdfFile)
pdfFile.close()

#local file
#pdfFile = open("../pages/warandpeace/chapter1.pdf", 'rb')

#translate the pdf
print("read over")

print(outputString)


import datetime

import telebot

import config

print("Starting at", datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))

bot = telebot.TeleBot(config.token)

def log(message, answer):
    print("\n ::::::::::")
    from datetime import datetime
    print(datetime.now())
    print("Сообщение от {0} {1}. (id = {2})\nТекст - {3}".format(message.from_user.first_name,
                                                                 message.from_user.last_name,
                                                                 str(message.from_user.id),
                                                                 message.text))
    print(answer)

@bot.message_handler(commands=['hello'])
def send_welcome(message):
    bot.reply_to(message, "Helloooo, how are you doing?")

@bot.message_handler(commands=['snail'])
def send_welcome(message):
    if message.from_user.id == 213701955:
        bot.reply_to(message, "I love u!!!! My little snail")
    log(message, '')

@bot.message_handler(commands=['register'])
def check(message):
    log(message, '')

@bot.message_handler(func=lambda message: True)
def echo_all(message):
    text = "Now i don't know how i can help u:("
    bot.reply_to(message, text)

if __name__ == '__main__':
    bot.polling(none_stop=True)


import openpyxl
import config
import math
import numpy
from sklearn import svm
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from sklearn import cross_validation
from sklearn.metrics import roc_curve, auc
from sklearn import preprocessing
from sklearn.grid_search import GridSearchCV
# from sklearn.cross_validation import cross_val_score
from sklearn.metrics import hamming_loss
import pylab as pl
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor
from datetime import datetime

configs = config.configs

# 记录程序开始执行时间
startTime = datetime.now()

# readBook = openpyxl.load_workbook(configs['condensedFeaturesPath'])
# readSheet = readBook.active

# read_book = openpyxl.load_workbook('/Users/ming.zhou/NLP/datasets/eliminateParaTag.xlsx')
# read_sheet = read_book.active

# readTestBook = openpyxl.load_workbook(configs['condensedTestFeaturesPath'])
# readTestSheet = readTestBook.active

# read_test_book = openpyxl.load_workbook('/Users/ming.zhou/NLP/datasets/eliminateParaTagTest.xlsx')
# read_test_sheet = read_test_book.active

read_all_book = openpyxl.load_workbook(configs['allFeatures_add40_Path'])
read_all_sheet = read_all_book.active

read_test2_book = openpyxl.load_workbook(configs['test_last_20_add40_Path'])
read_test2_sheet = read_test2_book.active

def get_feature_vector_and_tag():
    """
    获取训练集的句子特征向量以及句子标签
    :return:
        feature_vector_list：句子特征向量列表
        sentence_tag：句子标签
    """

    feature_vector_list = []
    sentence_tag = []
    for i in range(read_all_sheet.max_row - 1):

        feature_vector = []
        sentence_tag.append(read_all_sheet['E' + str(i + 2)].value)
        # 先添加段落特征
        feature_vector.append(float(read_all_sheet['A' + str(i + 2)].value.strip().split('-')[1]))
        for j in range(read_all_sheet.max_column - 5):
            if isinstance(read_all_sheet.cell(row=i + 2, column=j + 6).value, str):
                feature_vector.append(float(read_all_sheet.cell(row=i + 2, column=j + 6).value))
            else:
                feature_vector.append(read_all_sheet.cell(row=i + 2, column=j + 6).value)
        feature_vector_list.append(feature_vector)
    return feature_vector_list, sentence_tag

def get_tag_and_score(file_path, start, end, para_type='Introduction'):
    """
    获取训练集的句子标签以及段落分数
    :return:
    """
    read_book = openpyxl.load_workbook(file_path)
    read_sheet = read_book.active
    tags = []
    tag = []
    scores = []
    now_id = read_sheet['A2'].value
    sentence_tags = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
    max_length = 0

    for i in range(start, end):
        essay_id = read_sheet['A' + str(i)].value
        para_type_value = read_sheet['B' + str(i)].value
        #  判断是否是新的段落
        if essay_id != now_id:
            now_id = essay_id
            if len(tag) != 0:
                tags.append(tag)
                score = read_sheet['C' + str(i - 1)].value
                scores.append(score)
                # 保存最大的tag长度值
                if len(tag) > max_length:
                    max_length = len(tag)
                tag = []
        if para_type == para_type_value:
            tag.append(read_sheet['E' + str(i)].value)
    tag_features = []
    for tag in tags:
        tag_feature = []
        for sentence_tag in sentence_tags:
            sum = 0
            for t in tag:
                if sentence_tag == t:
                    sum += 1
            tag_feature.append(sum)
        tag_features.append(tag_feature)
    # 填充tag数据
    for tag in tags:
        if len(tag) < max_length:
            for j in range(max_length - len(tag)):
                tag.append(0)
    return tags, scores, tag_features

def get_test_feature_vector_and_tag():
    """
    获取测试集的句子特征向量以及句子标签
    :return:
        feature_vector_list：句子特征向量列表
        sentence_tag：句子标签
    """
    feature_vector_list = []
    sentence_tag = []
    for i in range(read_test2_sheet.max_row - 1):

        feature_vector = []
        sentence_tag.append(read_test2_sheet['E' + str(i + 2)].value)
        # 先添加段落特征
        feature_vector.append(float(read_test2_sheet['A' + str(i + 2)].value.strip().split('-')[1]))
        for j in range(read_test2_sheet.max_column - 5):
            if isinstance(read_test2_sheet.cell(row=i + 2, column=j + 6).value, str):
                feature_vector.append(float(read_test2_sheet.cell(row=i + 2, column=j + 6).value))
            else:
                feature_vector.append(read_test2_sheet.cell(row=i + 2, column=j + 6).value)

        # print(len(feature_vector))
        feature_vector_list.append(feature_vector)
    return feature_vector_list, sentence_tag

def adjust_parameter(x, y, flag='ExtraTrees'):
    """
    为模型调参
    :param x: 样本集的特征向量
    :param y: 样本集的标签列表
    :param flag: 模型指示词
    :return:
    """

    clf = ExtraTreesClassifier(min_samples_split=100, min_samples_leaf=20,
                               max_depth=8, max_features='sqrt', random_state=10)
    if flag == 'RandomForest':
        clf = RandomForestClassifier(n_estimators=10)
    elif flag == 'DecisionTree':
        clf = DecisionTreeClassifier()
    elif flag == 'SVM':
        clf = svm.SVC()
    param_grid = {
        'n_estimators': range(5, 20, 2)
    }
    grid_search = GridSearchCV(estimator=clf, param_grid=param_grid, scoring='roc_auc', cv=5)
    grid_search.fit(x, y)
    print(grid_search.grid_scores_)
    print(grid_search.best_params_)
    print(grid_search.best_score_)

def do_train(x, y, flag='RandomForest'):
    """
    训练并测试训练集的准确度
    :param x: 样本集的特征向量
    :param y: 样本集的标签列表
    :param flag: 模型指示词
    :return:
    """

    clf = RandomForestClassifier(n_estimators=10)
    if flag == 'ExtraTrees':
        clf = ExtraTreesClassifier(n_estimators=10)
    elif flag == 'DecisionTree':
        clf = DecisionTreeClassifier()
    elif flag == 'SVM':
        clf = svm.SVC()

    clf.fit(x, y)
    # clf = clf.fit(X, Y)

    y_pred = clf.predict(x)
    print(accuracy_score(y, y_pred))
    print(y)
    print(y_pred)
    print('Y len is %s，y_pred len is %s \n' % (len(y), len(y_pred)))
    # scores = cross_val_score(clf, X, Y)
    # print(scores.mean())

    compare_result = {}
    j = 0
    for i in range(len(y)):
        if y[i] == 5:
            j += 1
        y_and_ypred = str(y[i]) + '-' + str(y_pred[i])
        # print(y_and_ypred)
        if y[i] != y_pred[i]:
            if y_and_ypred not in compare_result:
                compare_result[y_and_ypred] = 1
            else:
                compare_result[y_and_ypred] = compare_result[y_and_ypred] + 1

    print(sorted(compare_result.items(), key=lambda d: -d[1]))

def do_train_by_test_set(train_x, train_y, test_x, test_y, flag='RandomForest'):
    """
    测试测试集的准确度
    :param train_x: 样本集中的特征向量列表
    :param train_y: 样本集中的标签列表
    :param test_x: 测试集中的特征向量列表
    :param test_y: 测试集中的标签列表
    :param flag: 模型指示词
    :return:
    """

    clf = RandomForestClassifier(n_estimators=10)
    file_path = '/Users/ming.zhou/NLP/DiscourseStructures/result/' + flag + 'Result20171115.text'

    if flag == 'ExtraTrees':
        clf = ExtraTreesClassifier(n_estimators=10)
    elif flag == 'DecisionTree':
        clf = DecisionTreeClassifier()
    elif flag == 'SVM':
        clf = svm.SVC()

    clf.fit(train_x, train_y)
    y_pred = clf.predict(test_x)

    result = open(file_path, 'a')
    result_content = list()
    result_content.append(str(clf.score(test_x, test_y)) + '\n')
    result_content.append('test_Y:' + '\n')
    result_content.append(str(test_y) + '\n')
    result_content.append('y_pred:' + '\n')
    result_content.append(str(y_pred) + '\n')
    result_content.append('test_Y len is ' + str(len(test_y)) + 'y_pred len is ' + str(len(y_pred)) + '\n')

    print(clf.score(test_x, test_y))
    print(accuracy_score(test_y, y_pred))
    print(test_y)
    print(y_pred)
    print('test_Y len is %s，y_pred len is %s \n' % (len(test_y), len(y_pred)))

    compare_result = {}
    for i in range(len(test_y)):
        y_and_ypred = str(test_y[i]) + '-' + str(y_pred[i])
        # print(y_and_ypred)
        if test_y[i] != y_pred[i]:
            if y_and_ypred not in compare_result:
                compare_result[y_and_ypred] = 1
            else:
                compare_result[y_and_ypred] = compare_result[y_and_ypred] + 1

    sorted_result = sorted(compare_result.items(), key=lambda d: -d[1])

    result_content.append(str(sorted_result) + '\n')
    print(sorted_result)
    result.writelines(result_content)
    result.close()

def do_train_by_cv(x, y, times_num, flag='ExtraTrees'):
    """
    交叉验证模型在训练集上的准确度
    :param x: 样本集中的特征向量列表
    :param y: 样本集中的标签列表
    :param times_num: 模型的n_estimators值
    :param flag: 模型指示词
    :return: 返回模型10次准确度的平均值
    """

    clf = ExtraTreesClassifier(n_estimators=times_num)
    file_path = '/Users/ming.zhou/NLP/DiscourseStructures/result/' + flag + 'CrossValidationResult20171122.text'

    result = open(file_path, 'a')
    result_content = list()
    result_content.append('**********************step=' + str(times_num) + '**********************' + '\n')
    if flag == 'RandomForest':
        clf = RandomForestClassifier(n_estimators=10)
    elif flag == 'DecisionTree':
        clf = DecisionTreeClassifier()
    elif flag == 'SVM':
        clf = svm.SVC()

    # 随机划分训练集与测试集，是交叉验证中常用的函数
    sum_num = 0
    for i in range(10):
        x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.1, random_state=i)
        clf.fit(x_train, y_train)
        # Y_pred = clf.predict(x_test)
        score = clf.score(x_test, y_test)
        result_content.append('index[' + str(i) + ']' + str(score) + '\n')
        print(score)

        sum_num += score
    result_content.append('平均准确度：' + str(sum_num/10) + '\n')
    result.writelines(result_content)
    result.close()
    print('平均准确度：', str(sum_num/10))
    return sum_num/10

def do_draw_train_auc(x, y, times_num, tag, flag='ExtraTrees'):
    """
    画出预测模型上的AUC图
    :param x: 样本集中的特征向量列表
    :param y: 样本集中的标签列表
    :param times_num: 模型的n_estimators值
    :param tag: 标签名
    :param flag: 模型指示词
    :return:
    """
    clf = ExtraTreesClassifier(n_estimators=times_num)

    if flag == 'RandomForest':
        clf = RandomForestClassifier(n_estimators=10)
    elif flag == 'DecisionTree':
        clf = DecisionTreeClassifier()
    elif flag == 'SVM':
        clf = svm.SVC()

    y_auc = []
    for c in y:
        if configs['Tags'][tag] == c:
            y_auc.append(1)
        else:
            y_auc.append(0)

    x_train, x_test, y_train, y_test = train_test_split(x, y_auc, test_size=0.1, random_state=0)
    clf.fit(x_train, y_train)
    # Y_pred = clf.predict(x_test)
    score = clf.score(x_test, y_test)
    print(score)
    fpr, tpr, threshold = roc_curve(y_test, score)
    roc_auc = auc(fpr, tpr)

    plt.figure()
    lw = 2
    plt.figure(figsize=(10, 10))
    plt.plot(fpr, tpr, color='darkorange',
             lw=lw, label='ROC curve (area = %0.2f)' % roc_auc)  ###假正率为横坐标，真正率为纵坐标做曲线
    plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
    plt.xlim([0.0, 1.0])
    plt.ylim([0.0, 1.05])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title('Receiver operating characteristic example')
    plt.legend(loc="lower right")
    plt.show()

def do_train_by_cv_and_norm(x, y, times_num, flag='ExtraTrees'):
    """
    使用交叉验证以及规范化技术训练模型在训练集上的准确度
    :param x: 样本集中的特征向量列表
    :param y: 样本集中的标签列表
    :param times_num: 模型的n_estimators值
    :param flag: 模型指示词
    :return:
    """
    clf = ExtraTreesClassifier(n_estimators=10)
    file_path = '/Users/ming.zhou/NLP/DiscourseStructures/result/' + flag + 'CrossValidationResult20171117.text'

    result = open(file_path, 'a')
    result_content = list()
    result_content.append('**********************step=' + str(times_num) + '**********************' + '\n')
    if flag == 'RandomForest':
        clf = RandomForestClassifier(n_estimators=10)
    elif flag == 'DecisionTree':
        clf = DecisionTreeClassifier()
    elif flag == 'SVM':
        clf = svm.SVC()

    # 随机划分训练集与测试集，是交叉验证中常用的函数
    sum_num = 0
    for i in range(10):
        x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.1, random_state=i)
        x_train_norm = preprocessing.normalize(x_train, norm='l2')
        x_test_norm = preprocessing.normalize(x_test, norm='l2')
        clf.fit(x_train_norm, y_train)
        # Y_pred = clf.predict(x_test)
        score = clf.score(x_test_norm, y_test)
        result_content.append('index[' + str(i) + ']' + str(score) + '\n')
        print(score)

        sum_num += score
    result_content.append('平均准确度：' + str(sum_num/10) + '\n')
    result.writelines(result_content)
    result.close()
    print('平均准确度：', str(sum_num/10))

def do_train_regression(x, y, times_num, flag='linear'):
    """
    训练评分回归模型
    :param x:
    :param y:
    :return:
    """
    rf = LinearRegression()
    if flag == 'RandomForest':
        rf = RandomForestRegressor(n_estimators=times_num)

    rf.fit(x, y)
    sum_num = 0
    for i in range(10):
        x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.1, random_state=i)
        rf.fit(x_train, y_train)
        # Y_pred = clf.predict(x_test)
        score = rf.score(x_test, y_test)
        sum_num += score
    return sum_num / 10

def do_predict(x_train, y_train, x_test, y_test, times_num, flag='ExtraTrees'):
    """
    预测模型在测试集上的准确度
    :param x_train: 样本集中的特征向量列表
    :param y_train: 样本集中的标签列表
    :param x_test: 测试集中的特征向量列表
    :param y_test: 测试集中的标签列表
    :param times_num: 模型的n_estimators值
    :param flag: 模型指示词
    :return: 返回预测的准确度
    """
    clf = ExtraTreesClassifier(n_estimators=times_num)
    file_path = '/Users/ming.zhou/NLP/DiscourseStructures/result/' + flag + 'PredictResult20171122.text'

    if flag == 'RandomForest':
        clf = RandomForestClassifier(n_estimators=10)
    elif flag == 'DecisionTree':
        clf = DecisionTreeClassifier()
    elif flag == 'SVM':
        clf = svm.SVC()

    clf.fit(x_train, y_train)
    y_pred = clf.predict(x_test)
    score = clf.score(x_test, y_test)

    # print(score)
    # print(Y_test)
    # print(y_pred)
    # print('test_Y len is %s，y_pred len is %s \n' %(len(Y_test), len(y_pred)))

    result = open(file_path, 'a')
    result_content = list()
    result_content.append('**********************step=' + str(times_num) + '**********************' + '\n')
    result_content.append(str(score) + '\n')
    result_content.append('test_Y:' + '\n')
    result_content.append(str(y_test) + '\n')
    result_content.append('y_pred:' + '\n')
    result_content.append(str(y_pred) + '\n')
    result_content.append('test_Y len is ' + str(len(y_test)) + '，and y_pred len is ' + str(len(y_pred)) + '\n')

    for i in range(len(y_test)):
        y_and_ypred = str(y_test[i]) + '-' + str(y_pred[i])
        # print(y_and_ypred)
        if y_test[i] != y_pred[i]:
            if y_and_ypred not in compare:
                compare[y_and_ypred] = 1
            else:
                compare[y_and_ypred] = compare[y_and_ypred] + 1

    # sortedResult = sorted(compare.items(), key=lambda d: -d[1])
    # print(sortedResult)
    # result_content.append(str(sortedResult) + '\n')
    result.writelines(result_content)
    result.close()
    return score

def do_draw_predict_auc(x_train, y_train, x_test, y_test, times_num, tag, flag='ExtraTrees'):
    """
    画出预测模型上的AUC图
    :param x_train: 样本集中的特征向量列表
    :param y_train: 样本集中的标签列表
    :param x_test: 测试集中的特征向量列表
    :param y_test: 测试集中的标签列表
    :param times_num: 模型的n_estimators值
    :param tag: 标签名
    :param flag: 模型指示词
    :return:
    """
    y_train_auc = []
    y_test_auc = []
    for tag_train in y_train:
        if configs['Tags'][tag] == tag_train:
            y_train_auc.append(1)
        else:
            y_train_auc.append(0)

    for tag_test in y_test:
        if configs['Tags'][tag] == tag_test:
            y_test_auc.append(1)
        else:
            y_test_auc.append(0)
    plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
    colors = ['magenta', 'green', 'red', 'cyan', 'blue', 'black']
    j = 0
    for i in range(10, 101, 20):
        clf = ExtraTreesClassifier(n_estimators=i)

        if flag == 'RandomForest':
            clf = RandomForestClassifier(n_estimators=10)
        elif flag == 'DecisionTree':
            clf = DecisionTreeClassifier()
        elif flag == 'SVM':
            clf = svm.SVC()

        clf.fit(x_train, y_train_auc)
        y_pred = clf.predict(x_test)
        fpr, tpr, threshold = roc_curve(y_test_auc, y_pred)
        roc_auc = auc(fpr, tpr)
        #  假正率为横坐标，真正率为纵坐标做曲线
        plt.plot(fpr, tpr, color=colors[j],
             lw=2, label='n_estimators %d ROC curve (area = %0.2f)' %(i, roc_auc))
        j += 1
    # y_score = clf.decision_function(x_test)
    # score = clf.score(x_test, y_test_auc)
    # print(y_pred)

    # plt.figure()
    # lw = 2
    # plt.figure(figsize=(10, 10))
    plt.xlim([0.0, 1.0])
    plt.ylim([0.0, 1.05])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title(tag + 'Receiver operating characteristic')
    plt.legend(loc="lower right")
    plt.show()

def do_predict_by_cv_and_norm(x_train, y_train, x_test, y_test, times_num, flag='ExtraTrees'):
    """
    使用规范化技术预测模型在测试集上的准确度
    :param x_train: 样本集中的特征向量列表
    :param y_train: 样本集中的标签列表
    :param x_test: 测试集中的特征向量列表
    :param y_test: 测试集中的标签列表
    :param times_num: 模型的n_estimators值
    :param flag: 模型指示词
    :return: 返回预测的准确度
    """
    clf = ExtraTreesClassifier(n_estimators=10)
    file_path = '/Users/ming.zhou/NLP/DiscourseStructures/result/' + flag + 'PredictResult20171117.text'

    if flag == 'RandomForest':
        clf = RandomForestClassifier(n_estimators=10)
    elif flag == 'DecisionTree':
        clf = DecisionTreeClassifier()
    elif flag == 'SVM':
        clf = svm.SVC()

    normalizer = preprocessing.Normalizer().fit(x_train)
    x_train_norm = normalizer.transform(x_train)
    x_test_norm = normalizer.transform(x_test)
    clf.fit(x_train_norm, y_train)
    y_pred = clf.predict(x_test_norm)
    score = clf.score(x_test_norm, y_test)

    print(str(score) + '\n')
    print(y_test + '\n')
    print(y_pred + '\n')
    print('test_Y len is %s，y_pred len is %s \n' % (len(y_test), len(y_pred)))

    result = open(file_path, 'a')
    result_content = list()
    result_content.append('**********************step=' + str(times_num) + '**********************' + '\n')
    result_content.append(str(score) + '\n')
    result_content.append('test_Y:' + '\n')
    result_content.append(str(y_test) + '\n')
    result_content.append('y_pred:' + '\n')
    result_content.append(str(y_pred) + '\n')
    result_content.append('test_Y len is ' + str(len(y_test)) + '，and y_pred len is ' + str(len(y_pred)) + '\n')

    for i in range(len(y_test)):
        y_and_ypred = str(y_test[i]) + '-' + str(y_pred[i])

        if y_test[i] != y_pred[i]:
            if y_and_ypred not in compare:
                compare[y_and_ypred] = 1
            else:
                compare[y_and_ypred] = compare[y_and_ypred] + 1

    # sortedResult = sorted(compare.items(), key=lambda d: -d[1])
    # print(sortedResult)
    # result_content.append(str(sortedResult) + '\n')
    result.writelines(result_content)
    result.close()
    return score

def do_draw_train_score(flag):
    """
    画训练集上评分准确率图
    :param flag: 标签名
    :return:
    """
    n = [62, 82, 92, 102, 112, 122]
    scores = []

    tag_and_score_init = get_tag_and_score(configs['new_scores_path'], 2, 1193, flag)
    tag_and_score_add20 = get_tag_and_score(configs['new_scores_path'], 2, 1614, flag)
    tag_and_score_add30 = get_tag_and_score(configs['new_scores_path'], 2, 1824, flag)
    tag_and_score_add40 = get_tag_and_score(configs['new_scores_path'], 2, 2005, flag)
    tag_and_score_add50 = get_tag_and_score(configs['new_scores_path'], 2, 2191, flag)
    tag_and_score_add60 = get_tag_and_score(configs['new_scores_path'], 2, 2414, flag)

    scores.append(do_train_regression(
        tag_and_score_init[2], tag_and_score_init[1], 0))
    scores.append(do_train_regression(
        tag_and_score_add20[2], tag_and_score_add20[1], 0))
    scores.append(do_train_regression(
        tag_and_score_add30[2], tag_and_score_add30[1], 0))
    scores.append(do_train_regression(
        tag_and_score_add40[2], tag_and_score_add40[1], 0))
    scores.append(do_train_regression(
        tag_and_score_add50[2], tag_and_score_add50[1], 0))
    scores.append(do_train_regression(
        tag_and_score_add60[2], tag_and_score_add60[1], 0))

    pl.plot(n, scores, 'g.-')
    pl.title('Linear ' + flag + ' train result')
    pl.xlabel('train set size')
    pl.ylabel('score')
    pl.show()

    # pl.plot(n, score_reason, 'r.-')
    # pl.plot(n, score_concession, 'y.-')
    # pl.plot(n, score_conclusion, 'k.-')
    # # pl.plot(n, score_add50, 'c.-')
    # # pl.plot(n, score_add60, 'b.-')
    # green_patch = mpatches.Patch(color='green', label='introduction score')
    # red_patch = mpatches.Patch(color='red', label='reason score')
    # yellow_patch = mpatches.Patch(color='yellow', label='concession score')
    # black_patch = mpatches.Patch(color='black', label='conclusion score')
    # # cyan_patch = mpatches.Patch(color='cyan', label='add 50 train set')
    # # blue_patch = mpatches.Patch(color='blue', label='add 60 train set')
    # pl.legend(handles=[green_patch, red_patch, yellow_patch, black_patch])
    # pl.title('Linear train result')
    # pl.xlabel('train set size')
    # pl.ylabel('score')
    # pl.show()

params = get_feature_vector_and_tag()
testParams = get_test_feature_vector_and_tag()
# doTrain(params[0], params[1], 'ExtraTrees')
# doTrainByTestSet(params[0], params[1], testParams[0], testParams[1], 'ExtraTrees')
# doTrainByCrossValidation(params[0], params[1], 10, 'ExtraTrees')
# adjustParameter(params[0], params[1])
# doTrainByCVAndNorm(params[0], params[1], 2, 'ExtraTrees')
# print('*********************begin test*********************')
compare = {}

# xs = []
# ys = []
# for index in range(10, 101, 5):
    # xs.append(index)
    # scores = 0
    # for j in range(3):
    #     score = do_predict(params[0], params[1], testParams[0], testParams[1], index, 'ExtraTrees')
    #     scores += score
    # s = do_train_by_cv(params[0], params[1], index, 'ExtraTrees')
    # ys.append(scores/3)
    # ys.append(s)
    # print('index[' + str(index) + ']:' + str(scores/3))
    # print('index[' + str(index) + ']:' + str(s))

# scores = 0
# for j in range(10):
#     score = do_predict(params[0], params[1], testParams[0], testParams[1], 80, 'ExtraTrees')
#     scores += score
# print(scores / 10)

# sortedResult = sorted(compare.items(), key=lambda d: -d[1])
# print(sortedResult)
# print(scores / 30)

# 判断结构评分的数据分布
# y_add60 = get_tag_and_score(configs['allFeatures_add60_Path'], 'Conclusion')[1]
# print(y_add60)
# pl.scatter(y_add60, y_add60, c='green')
# pl.title('Conclusion Scatter Figure')
# pl.xlabel('x')
# pl.ylabel('y')
# pl.legend()
# pl.grid(True)
# pl.show()

# do_draw_train_auc(params[0], params[1], 80, '<EG>')
do_draw_predict_auc(params[0], params[1], testParams[0], testParams[1], 0, '<TST>')
# do_draw_train_score('Reason')
# 计算程序运行总时间(秒)
elapsed = (datetime.now() - startTime).seconds
print('Time used : ', elapsed)


import cv2
import numpy as np
import os
from pylsd import lsd
fullName = 'car.jpg'
folder, imgName = os.path.split(fullName)
src = cv2.imread(fullName, cv2.IMREAD_COLOR)
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
lines = lsd(gray)
for i in range(lines.shape[0]):
    pt1 = (int(lines[i, 0]), int(lines[i, 1]))
    pt2 = (int(lines[i, 2]), int(lines[i, 3]))
    width = lines[i, 4]
    cv2.line(src, pt1, pt2, (0, 0, 255), int(np.ceil(width / 2)))
cv2.imwrite(os.path.join(folder, 'cv2_' + imgName.split('.')[0] + '.jpg'), src)

cv2.imshow('Segmentos de línea', src)
cv2.waitKey(0)
cv2.destroyAllWindows()


from django.db import IntegrityError
from django.http import HttpResponseRedirect
from django.shortcuts import render

from firstapp.getfunctions import get_cn
from firstapp.models import RegistrationAdmin

def add_admin(request):
    if request.method == "GET" and request.session['username'] == 'admin':
        people = RegistrationAdmin.objects.all()
        return render(request, "admin/registration_admin.html", {"people": people})
    if request.method == "POST" and request.session['username'] == 'admin':
        try:
            admin = RegistrationAdmin()
            admin.fullname = get_cn(request, request.POST.get("username"))
            admin.username = request.POST.get("username").lower()
            admin.save()
        except IntegrityError:
            return render(request, "error.html", {"error": "Integrity Error"})
        return HttpResponseRedirect("/registration_admin")

def delete_admin(request, id):
    if request.session['username'] == 'admin':
        try:
            admin = RegistrationAdmin.objects.get(id=id)
            admin.delete()
            return HttpResponseRedirect("/registration_admin")
        except RegistrationAdmin.DoesNotExist:
            return render(request, "error.html")